nat: remove non-error error counters
[vpp.git] / src / plugins / nat / in2out.c
1 /*
2  * Copyright (c) 2016 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 inside to outside network translation
18  */
19
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/pg/pg.h>
23
24 #include <vnet/ip/ip.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/fib/ip4_fib.h>
27 #include <vnet/udp/udp.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 #include <vppinfra/hash.h>
36 #include <vppinfra/error.h>
37 #include <vppinfra/elog.h>
38 #include <nat/lib/nat_inlines.h>
39
40 typedef struct
41 {
42   u32 sw_if_index;
43   u32 next_index;
44   u32 session_index;
45   u32 is_slow_path;
46   u32 is_hairpinning;
47 } snat_in2out_trace_t;
48
49 /* packet trace format function */
50 static u8 *
51 format_snat_in2out_trace (u8 * s, va_list * args)
52 {
53   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
54   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
55   snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
56   char *tag;
57
58   tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
59
60   s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
61               t->sw_if_index, t->next_index, t->session_index);
62   if (t->is_hairpinning)
63     {
64       s = format (s, ", with-hairpinning");
65     }
66
67   return s;
68 }
69
70 static u8 *
71 format_snat_in2out_fast_trace (u8 * s, va_list * args)
72 {
73   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
74   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
75   snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
76
77   s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
78               t->sw_if_index, t->next_index);
79
80   return s;
81 }
82
83 #define foreach_snat_in2out_error                       \
84 _(UNSUPPORTED_PROTOCOL, "unsupported protocol")         \
85 _(OUT_OF_PORTS, "out of ports")                         \
86 _(BAD_OUTSIDE_FIB, "outside VRF ID not found")          \
87 _(BAD_ICMP_TYPE, "unsupported ICMP type")               \
88 _(NO_TRANSLATION, "no translation")                     \
89 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")   \
90 _(CANNOT_CREATE_USER, "cannot create NAT user")
91
92 typedef enum
93 {
94 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
95   foreach_snat_in2out_error
96 #undef _
97     SNAT_IN2OUT_N_ERROR,
98 } snat_in2out_error_t;
99
100 static char *snat_in2out_error_strings[] = {
101 #define _(sym,string) string,
102   foreach_snat_in2out_error
103 #undef _
104 };
105
106 typedef enum
107 {
108   SNAT_IN2OUT_NEXT_LOOKUP,
109   SNAT_IN2OUT_NEXT_DROP,
110   SNAT_IN2OUT_NEXT_ICMP_ERROR,
111   SNAT_IN2OUT_NEXT_SLOW_PATH,
112   SNAT_IN2OUT_N_NEXT,
113 } snat_in2out_next_t;
114
115 static inline int
116 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
117                     u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
118                     u32 rx_fib_index0, u32 thread_index)
119 {
120   udp_header_t *udp0 = ip4_next_header (ip0);
121   clib_bihash_kv_8_8_t kv0, value0;
122
123   init_nat_k (&kv0, ip0->dst_address, udp0->dst_port, sm->outside_fib_index,
124               proto0);
125
126   /* NAT packet aimed at external address if */
127   /* has active sessions */
128   if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
129                               &value0))
130     {
131       /* or is static mappings */
132       ip4_address_t placeholder_addr;
133       u16 placeholder_port;
134       u32 placeholder_fib_index;
135       if (!snat_static_mapping_match
136           (sm, ip0->dst_address, udp0->dst_port, sm->outside_fib_index,
137            proto0, &placeholder_addr, &placeholder_port,
138            &placeholder_fib_index, 1, 0, 0, 0, 0, 0))
139         return 0;
140     }
141   else
142     return 0;
143
144   if (sm->forwarding_enabled)
145     return 1;
146
147   return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
148                                   rx_fib_index0);
149 }
150
151 static inline int
152 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
153                                   u32 proto0, u16 src_port, u16 dst_port,
154                                   u32 thread_index, u32 sw_if_index)
155 {
156   clib_bihash_kv_8_8_t kv0, value0;
157   snat_interface_t *i;
158
159   /* src NAT check */
160   init_nat_k (&kv0, ip0->src_address, src_port,
161               ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
162
163   if (!clib_bihash_search_8_8
164       (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
165     return 1;
166
167   /* dst NAT check */
168   init_nat_k (&kv0, ip0->dst_address, dst_port,
169               ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
170   if (!clib_bihash_search_8_8
171       (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
172     {
173       /* hairpinning */
174     /* *INDENT-OFF* */
175     pool_foreach (i, sm->output_feature_interfaces,
176     ({
177       if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
178         return 0;
179     }));
180     /* *INDENT-ON* */
181       return 1;
182     }
183
184   return 0;
185 }
186
187 #ifndef CLIB_MARCH_VARIANT
188 int
189 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
190 {
191   snat_main_t *sm = &snat_main;
192   nat44_is_idle_session_ctx_t *ctx = arg;
193   snat_session_t *s;
194   u64 sess_timeout_time;
195   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
196                                                        ctx->thread_index);
197   clib_bihash_kv_8_8_t s_kv;
198
199   s = pool_elt_at_index (tsm->sessions, kv->value);
200   sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
201   if (ctx->now >= sess_timeout_time)
202     {
203       init_nat_o2i_k (&s_kv, s);
204       if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
205         nat_elog_warn ("out2in key del failed");
206
207       snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
208                                            s->in2out.addr.as_u32,
209                                            s->out2in.addr.as_u32,
210                                            s->nat_proto,
211                                            s->in2out.port,
212                                            s->out2in.port,
213                                            s->in2out.fib_index);
214
215       nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
216                                &s->in2out.addr, s->in2out.port,
217                                &s->out2in.addr, s->out2in.port, s->nat_proto);
218
219       nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
220                    s->ext_host_port, s->nat_proto, s->out2in.fib_index,
221                    ctx->thread_index);
222
223       if (!snat_is_session_static (s))
224         snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
225                                             &s->out2in.addr,
226                                             s->out2in.port, s->nat_proto);
227
228       nat44_delete_session (sm, s, ctx->thread_index);
229       return 1;
230     }
231
232   return 0;
233 }
234 #endif
235
236 static u32
237 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
238            ip4_header_t * ip0,
239            ip4_address_t i2o_addr,
240            u16 i2o_port,
241            u32 rx_fib_index0,
242            nat_protocol_t nat_proto,
243            snat_session_t ** sessionp,
244            vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
245 {
246   snat_user_t *u;
247   snat_session_t *s = 0;
248   clib_bihash_kv_8_8_t kv0;
249   u8 is_sm = 0;
250   nat_outside_fib_t *outside_fib;
251   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
252   u8 identity_nat;
253   fib_prefix_t pfx = {
254     .fp_proto = FIB_PROTOCOL_IP4,
255     .fp_len = 32,
256     .fp_addr = {
257                 .ip4.as_u32 = ip0->dst_address.as_u32,
258                 },
259   };
260   nat44_is_idle_session_ctx_t ctx0;
261   ip4_address_t sm_addr;
262   u16 sm_port;
263   u32 sm_fib_index;
264
265   if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
266     {
267       b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
268       nat_ipfix_logging_max_sessions (thread_index,
269                                       sm->max_translations_per_thread);
270       nat_elog_notice ("maximum sessions exceeded");
271       return SNAT_IN2OUT_NEXT_DROP;
272     }
273
274   /* First try to match static mapping by local address and port */
275   if (snat_static_mapping_match
276       (sm, i2o_addr, i2o_port, rx_fib_index0, nat_proto, &sm_addr,
277        &sm_port, &sm_fib_index, 0, 0, 0, 0, 0, &identity_nat))
278     {
279       /* Try to create dynamic translation */
280       if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
281                                                thread_index,
282                                                nat_proto,
283                                                &sm_addr, &sm_port,
284                                                sm->port_per_thread,
285                                                sm->per_thread_data
286                                                [thread_index].snat_thread_index))
287         {
288           b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
289           return SNAT_IN2OUT_NEXT_DROP;
290         }
291     }
292   else
293     {
294       if (PREDICT_FALSE (identity_nat))
295         {
296           *sessionp = s;
297           return next0;
298         }
299
300       is_sm = 1;
301     }
302
303   u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
304                               thread_index);
305   if (!u)
306     {
307       b0->error = node->errors[SNAT_IN2OUT_ERROR_CANNOT_CREATE_USER];
308       return SNAT_IN2OUT_NEXT_DROP;
309     }
310
311   s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
312   if (!s)
313     {
314       nat44_delete_user_with_no_session (sm, u, thread_index);
315       nat_elog_warn ("create NAT session failed");
316       return SNAT_IN2OUT_NEXT_DROP;
317     }
318
319   if (is_sm)
320     s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
321   user_session_increment (sm, u, is_sm);
322   s->in2out.addr = i2o_addr;
323   s->in2out.port = i2o_port;
324   s->in2out.fib_index = rx_fib_index0;
325   s->nat_proto = nat_proto;
326   s->out2in.addr = sm_addr;
327   s->out2in.port = sm_port;
328   s->out2in.fib_index = sm->outside_fib_index;
329   switch (vec_len (sm->outside_fibs))
330     {
331     case 0:
332       s->out2in.fib_index = sm->outside_fib_index;
333       break;
334     case 1:
335       s->out2in.fib_index = sm->outside_fibs[0].fib_index;
336       break;
337     default:
338       /* *INDENT-OFF* */
339       vec_foreach (outside_fib, sm->outside_fibs)
340         {
341           fei = fib_table_lookup (outside_fib->fib_index, &pfx);
342           if (FIB_NODE_INDEX_INVALID != fei)
343             {
344               if (fib_entry_get_resolving_interface (fei) != ~0)
345                 {
346                   s->out2in.fib_index = outside_fib->fib_index;
347                   break;
348                 }
349             }
350         }
351       /* *INDENT-ON* */
352       break;
353     }
354   s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
355   s->ext_host_port = vnet_buffer (b0)->ip.reass.l4_dst_port;
356   *sessionp = s;
357
358   /* Add to translation hashes */
359   ctx0.now = now;
360   ctx0.thread_index = thread_index;
361   init_nat_i2o_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
362   if (clib_bihash_add_or_overwrite_stale_8_8
363       (&sm->per_thread_data[thread_index].in2out, &kv0,
364        nat44_i2o_is_idle_session_cb, &ctx0))
365     nat_elog_notice ("in2out key add failed");
366
367   init_nat_o2i_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
368   if (clib_bihash_add_or_overwrite_stale_8_8
369       (&sm->per_thread_data[thread_index].out2in, &kv0,
370        nat44_o2i_is_idle_session_cb, &ctx0))
371     nat_elog_notice ("out2in key add failed");
372
373   /* log NAT event */
374   snat_ipfix_logging_nat44_ses_create (thread_index,
375                                        s->in2out.addr.as_u32,
376                                        s->out2in.addr.as_u32,
377                                        s->nat_proto,
378                                        s->in2out.port,
379                                        s->out2in.port, s->in2out.fib_index);
380
381   nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
382                            &s->in2out.addr, s->in2out.port, &s->out2in.addr,
383                            s->out2in.port, s->nat_proto);
384
385   nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
386                s->out2in.port, &s->ext_host_addr, s->ext_host_port,
387                &s->ext_host_nat_addr, s->ext_host_nat_port,
388                s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
389
390   return next0;
391 }
392
393 #ifndef CLIB_MARCH_VARIANT
394 static_always_inline snat_in2out_error_t
395 icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
396               ip4_address_t * addr, u16 * port, nat_protocol_t * nat_proto)
397 {
398   icmp46_header_t *icmp0;
399   icmp_echo_header_t *echo0, *inner_echo0 = 0;
400   ip4_header_t *inner_ip0 = 0;
401   void *l4_header = 0;
402   icmp46_header_t *inner_icmp0;
403
404   icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
405   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
406
407   if (!icmp_type_is_error_message
408       (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
409     {
410       *nat_proto = NAT_PROTOCOL_ICMP;
411       *addr = ip0->src_address;
412       *port = vnet_buffer (b)->ip.reass.l4_src_port;
413     }
414   else
415     {
416       inner_ip0 = (ip4_header_t *) (echo0 + 1);
417       l4_header = ip4_next_header (inner_ip0);
418       *nat_proto = ip_proto_to_nat_proto (inner_ip0->protocol);
419       *addr = inner_ip0->dst_address;
420       switch (*nat_proto)
421         {
422         case NAT_PROTOCOL_ICMP:
423           inner_icmp0 = (icmp46_header_t *) l4_header;
424           inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
425           *port = inner_echo0->identifier;
426           break;
427         case NAT_PROTOCOL_UDP:
428         case NAT_PROTOCOL_TCP:
429           *port = ((tcp_udp_header_t *) l4_header)->dst_port;
430           break;
431         default:
432           return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
433         }
434     }
435   return -1;                    /* success */
436 }
437
438 /**
439  * Get address and port values to be used for ICMP packet translation
440  * and create session if needed
441  *
442  * @param[in,out] sm             NAT main
443  * @param[in,out] node           NAT node runtime
444  * @param[in] thread_index       thread index
445  * @param[in,out] b0             buffer containing packet to be translated
446  * @param[in,out] ip0            ip header
447  * @param[out] p_proto           protocol used for matching
448  * @param[out] p_value           address and port after NAT translation
449  * @param[out] p_dont_translate  if packet should not be translated
450  * @param d                      optional parameter
451  * @param e                      optional parameter
452  */
453 u32
454 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
455                         u32 thread_index, vlib_buffer_t * b0,
456                         ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
457                         u32 * fib_index, nat_protocol_t * proto, void *d,
458                         void *e, u8 * dont_translate)
459 {
460   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
461   u32 sw_if_index0;
462   snat_session_t *s0 = 0;
463   clib_bihash_kv_8_8_t kv0, value0;
464   u32 next0 = ~0;
465   int err;
466   vlib_main_t *vm = vlib_get_main ();
467   *dont_translate = 0;
468
469   sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
470   *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
471
472   err = icmp_get_key (b0, ip0, addr, port, proto);
473   if (err != -1)
474     {
475       b0->error = node->errors[err];
476       next0 = SNAT_IN2OUT_NEXT_DROP;
477       goto out;
478     }
479
480   init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
481   if (clib_bihash_search_8_8 (&tsm->in2out, &kv0, &value0))
482     {
483       if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
484         {
485           if (PREDICT_FALSE
486               (nat_not_translate_output_feature
487                (sm, ip0, *proto, *port, *port, thread_index, sw_if_index0)))
488             {
489               *dont_translate = 1;
490               goto out;
491             }
492         }
493       else
494         {
495           if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
496                                                  ip0, NAT_PROTOCOL_ICMP,
497                                                  *fib_index, thread_index)))
498             {
499               *dont_translate = 1;
500               goto out;
501             }
502         }
503
504       if (PREDICT_FALSE
505           (icmp_type_is_error_message
506            (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags)))
507         {
508           b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
509           next0 = SNAT_IN2OUT_NEXT_DROP;
510           goto out;
511         }
512
513       next0 =
514         slow_path (sm, b0, ip0, *addr, *port, *fib_index, *proto, &s0, node,
515                    next0, thread_index, vlib_time_now (vm));
516
517       if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
518         goto out;
519
520       if (!s0)
521         {
522           *dont_translate = 1;
523           goto out;
524         }
525     }
526   else
527     {
528       if (PREDICT_FALSE
529           (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
530            ICMP4_echo_request
531            && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
532            ICMP4_echo_reply
533            && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
534                                            reass.icmp_type_or_tcp_flags)))
535         {
536           b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
537           next0 = SNAT_IN2OUT_NEXT_DROP;
538           goto out;
539         }
540
541       s0 = pool_elt_at_index (tsm->sessions, value0.value);
542     }
543
544 out:
545   if (s0)
546     {
547       *addr = s0->out2in.addr;
548       *port = s0->out2in.port;
549       *fib_index = s0->out2in.fib_index;
550     }
551   if (d)
552     *(snat_session_t **) (d) = s0;
553   return next0;
554 }
555 #endif
556
557 #ifndef CLIB_MARCH_VARIANT
558 /**
559  * Get address and port values to be used for ICMP packet translation
560  *
561  * @param[in] sm                 NAT main
562  * @param[in,out] node           NAT node runtime
563  * @param[in] thread_index       thread index
564  * @param[in,out] b0             buffer containing packet to be translated
565  * @param[in,out] ip0            ip header
566  * @param[out] p_proto           protocol used for matching
567  * @param[out] p_value           address and port after NAT translation
568  * @param[out] p_dont_translate  if packet should not be translated
569  * @param d                      optional parameter
570  * @param e                      optional parameter
571  */
572 u32
573 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
574                         u32 thread_index, vlib_buffer_t * b0,
575                         ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
576                         u32 * fib_index, nat_protocol_t * proto, void *d,
577                         void *e, u8 * dont_translate)
578 {
579   u32 sw_if_index0;
580   u8 is_addr_only;
581   u32 next0 = ~0;
582   int err;
583   *dont_translate = 0;
584
585   sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
586   *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
587
588   err = icmp_get_key (b0, ip0, addr, port, proto);
589   if (err != -1)
590     {
591       b0->error = node->errors[err];
592       next0 = SNAT_IN2OUT_NEXT_DROP;
593       goto out;
594     }
595
596   ip4_address_t sm_addr;
597   u16 sm_port;
598   u32 sm_fib_index;
599
600   if (snat_static_mapping_match
601       (sm, *addr, *port, *fib_index, *proto, &sm_addr, &sm_port,
602        &sm_fib_index, 0, &is_addr_only, 0, 0, 0, 0))
603     {
604       if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
605                                                   IP_PROTOCOL_ICMP,
606                                                   *fib_index)))
607         {
608           *dont_translate = 1;
609           goto out;
610         }
611
612       if (icmp_type_is_error_message
613           (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
614         {
615           next0 = SNAT_IN2OUT_NEXT_DROP;
616           goto out;
617         }
618
619       b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
620       next0 = SNAT_IN2OUT_NEXT_DROP;
621       goto out;
622     }
623
624   if (PREDICT_FALSE
625       (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_request
626        && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
627            ICMP4_echo_reply || !is_addr_only)
628        && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
629                                        reass.icmp_type_or_tcp_flags)))
630     {
631       b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
632       next0 = SNAT_IN2OUT_NEXT_DROP;
633       goto out;
634     }
635
636 out:
637   return next0;
638 }
639 #endif
640
641 #ifndef CLIB_MARCH_VARIANT
642 u32
643 icmp_in2out (snat_main_t * sm,
644              vlib_buffer_t * b0,
645              ip4_header_t * ip0,
646              icmp46_header_t * icmp0,
647              u32 sw_if_index0,
648              u32 rx_fib_index0,
649              vlib_node_runtime_t * node,
650              u32 next0, u32 thread_index, void *d, void *e)
651 {
652   vlib_main_t *vm = vlib_get_main ();
653   ip4_address_t addr;
654   u16 port;
655   u32 fib_index;
656   nat_protocol_t protocol;
657   icmp_echo_header_t *echo0, *inner_echo0 = 0;
658   ip4_header_t *inner_ip0;
659   void *l4_header = 0;
660   icmp46_header_t *inner_icmp0;
661   u8 dont_translate;
662   u32 new_addr0, old_addr0;
663   u16 old_id0, new_id0;
664   u16 old_checksum0, new_checksum0;
665   ip_csum_t sum0;
666   u16 checksum0;
667   u32 next0_tmp;
668
669   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
670
671   next0_tmp =
672     sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0, &addr, &port,
673                               &fib_index, &protocol, d, e, &dont_translate);
674   if (next0_tmp != ~0)
675     next0 = next0_tmp;
676   if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
677     goto out;
678
679   if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
680     {
681       sum0 =
682         ip_incremental_checksum_buffer (vm, b0,
683                                         (u8 *) icmp0 -
684                                         (u8 *) vlib_buffer_get_current (b0),
685                                         ntohs (ip0->length) -
686                                         ip4_header_bytes (ip0), 0);
687       checksum0 = ~ip_csum_fold (sum0);
688       if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
689         {
690           next0 = SNAT_IN2OUT_NEXT_DROP;
691           goto out;
692         }
693     }
694
695   old_addr0 = ip0->src_address.as_u32;
696   new_addr0 = ip0->src_address.as_u32 = addr.as_u32;
697
698   sum0 = ip0->checksum;
699   sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
700                          src_address /* changed member */ );
701   ip0->checksum = ip_csum_fold (sum0);
702
703   if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
704     {
705       if (icmp0->checksum == 0)
706         icmp0->checksum = 0xffff;
707
708       if (!icmp_type_is_error_message (icmp0->type))
709         {
710           new_id0 = port;
711           if (PREDICT_FALSE (new_id0 != echo0->identifier))
712             {
713               old_id0 = echo0->identifier;
714               new_id0 = port;
715               echo0->identifier = new_id0;
716
717               sum0 = icmp0->checksum;
718               sum0 =
719                 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
720                                 identifier);
721               icmp0->checksum = ip_csum_fold (sum0);
722             }
723         }
724       else
725         {
726           inner_ip0 = (ip4_header_t *) (echo0 + 1);
727           l4_header = ip4_next_header (inner_ip0);
728
729           if (!ip4_header_checksum_is_valid (inner_ip0))
730             {
731               next0 = SNAT_IN2OUT_NEXT_DROP;
732               goto out;
733             }
734
735           /* update inner destination IP address */
736           old_addr0 = inner_ip0->dst_address.as_u32;
737           inner_ip0->dst_address = addr;
738           new_addr0 = inner_ip0->dst_address.as_u32;
739           sum0 = icmp0->checksum;
740           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
741                                  dst_address /* changed member */ );
742           icmp0->checksum = ip_csum_fold (sum0);
743
744           /* update inner IP header checksum */
745           old_checksum0 = inner_ip0->checksum;
746           sum0 = inner_ip0->checksum;
747           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
748                                  dst_address /* changed member */ );
749           inner_ip0->checksum = ip_csum_fold (sum0);
750           new_checksum0 = inner_ip0->checksum;
751           sum0 = icmp0->checksum;
752           sum0 =
753             ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
754                             checksum);
755           icmp0->checksum = ip_csum_fold (sum0);
756
757           switch (protocol)
758             {
759             case NAT_PROTOCOL_ICMP:
760               inner_icmp0 = (icmp46_header_t *) l4_header;
761               inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
762
763               old_id0 = inner_echo0->identifier;
764               new_id0 = port;
765               inner_echo0->identifier = new_id0;
766
767               sum0 = icmp0->checksum;
768               sum0 =
769                 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
770                                 identifier);
771               icmp0->checksum = ip_csum_fold (sum0);
772               break;
773             case NAT_PROTOCOL_UDP:
774             case NAT_PROTOCOL_TCP:
775               old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
776               new_id0 = port;
777               ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
778
779               sum0 = icmp0->checksum;
780               sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
781                                      dst_port);
782               icmp0->checksum = ip_csum_fold (sum0);
783               break;
784             default:
785               ASSERT (0);
786             }
787         }
788     }
789
790   if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
791     {
792       if (sm->deterministic ||
793           0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
794                                       sm->endpoint_dependent))
795         vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
796     }
797
798 out:
799   return next0;
800 }
801 #endif
802
803 static inline u32
804 icmp_in2out_slow_path (snat_main_t * sm,
805                        vlib_buffer_t * b0,
806                        ip4_header_t * ip0,
807                        icmp46_header_t * icmp0,
808                        u32 sw_if_index0,
809                        u32 rx_fib_index0,
810                        vlib_node_runtime_t * node,
811                        u32 next0,
812                        f64 now, u32 thread_index, snat_session_t ** p_s0)
813 {
814   vlib_main_t *vm = vlib_get_main ();
815
816   next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
817                        next0, thread_index, p_s0, 0);
818   snat_session_t *s0 = *p_s0;
819   if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
820     {
821       /* Accounting */
822       nat44_session_update_counters (s0, now,
823                                      vlib_buffer_length_in_chain
824                                      (vm, b0), thread_index);
825       /* Per-user LRU list maintenance */
826       nat44_session_update_lru (sm, s0, thread_index);
827     }
828   return next0;
829 }
830
831 static int
832 nat_in2out_sm_unknown_proto (snat_main_t * sm,
833                              vlib_buffer_t * b,
834                              ip4_header_t * ip, u32 rx_fib_index)
835 {
836   clib_bihash_kv_8_8_t kv, value;
837   snat_static_mapping_t *m;
838   u32 old_addr, new_addr;
839   ip_csum_t sum;
840
841   init_nat_k (&kv, ip->src_address, 0, rx_fib_index, 0);
842   if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
843     return 1;
844
845   m = pool_elt_at_index (sm->static_mappings, value.value);
846
847   old_addr = ip->src_address.as_u32;
848   new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
849   sum = ip->checksum;
850   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
851   ip->checksum = ip_csum_fold (sum);
852
853
854   /* Hairpinning */
855   if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
856     {
857       vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
858       nat_hairpinning_sm_unknown_proto (sm, b, ip);
859     }
860
861   return 0;
862 }
863
864 static inline uword
865 snat_in2out_node_fn_inline (vlib_main_t * vm,
866                             vlib_node_runtime_t * node,
867                             vlib_frame_t * frame, int is_slow_path,
868                             int is_output_feature)
869 {
870   u32 n_left_from, *from;
871   snat_main_t *sm = &snat_main;
872   f64 now = vlib_time_now (vm);
873   u32 thread_index = vm->thread_index;
874
875   from = vlib_frame_vector_args (frame);
876   n_left_from = frame->n_vectors;
877
878   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
879   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
880   vlib_get_buffers (vm, from, b, n_left_from);
881
882   while (n_left_from >= 2)
883     {
884       vlib_buffer_t *b0, *b1;
885       u32 next0, next1;
886       u32 sw_if_index0, sw_if_index1;
887       ip4_header_t *ip0, *ip1;
888       ip_csum_t sum0, sum1;
889       u32 new_addr0, old_addr0, new_addr1, old_addr1;
890       u16 old_port0, new_port0, old_port1, new_port1;
891       udp_header_t *udp0, *udp1;
892       tcp_header_t *tcp0, *tcp1;
893       icmp46_header_t *icmp0, *icmp1;
894       u32 rx_fib_index0, rx_fib_index1;
895       u32 proto0, proto1;
896       snat_session_t *s0 = 0, *s1 = 0;
897       clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
898       u32 iph_offset0 = 0, iph_offset1 = 0;
899
900       b0 = *b;
901       b++;
902       b1 = *b;
903       b++;
904
905       /* Prefetch next iteration. */
906       if (PREDICT_TRUE (n_left_from >= 4))
907         {
908           vlib_buffer_t *p2, *p3;
909
910           p2 = *b;
911           p3 = *(b + 1);
912
913           vlib_prefetch_buffer_header (p2, LOAD);
914           vlib_prefetch_buffer_header (p3, LOAD);
915
916           CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
917           CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
918         }
919
920       if (is_output_feature)
921         iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
922
923       ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
924                               iph_offset0);
925
926       udp0 = ip4_next_header (ip0);
927       tcp0 = (tcp_header_t *) udp0;
928       icmp0 = (icmp46_header_t *) udp0;
929
930       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
931       rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
932                                sw_if_index0);
933
934       next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
935
936       if (PREDICT_FALSE (ip0->ttl == 1))
937         {
938           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
939           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
940                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
941                                        0);
942           next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
943           goto trace00;
944         }
945
946       proto0 = ip_proto_to_nat_proto (ip0->protocol);
947
948       /* Next configured feature, probably ip4-lookup */
949       if (is_slow_path)
950         {
951           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
952             {
953               if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
954                 {
955                   next0 = SNAT_IN2OUT_NEXT_DROP;
956                   b0->error =
957                     node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
958                 }
959               vlib_increment_simple_counter (is_slow_path ? &sm->
960                                              counters.slowpath.in2out.
961                                              other : &sm->counters.fastpath.
962                                              in2out.other, thread_index,
963                                              sw_if_index0, 1);
964               goto trace00;
965             }
966
967           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
968             {
969               next0 = icmp_in2out_slow_path
970                 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
971                  node, next0, now, thread_index, &s0);
972               vlib_increment_simple_counter (is_slow_path ? &sm->
973                                              counters.slowpath.in2out.
974                                              icmp : &sm->counters.fastpath.
975                                              in2out.icmp, thread_index,
976                                              sw_if_index0, 1);
977               goto trace00;
978             }
979         }
980       else
981         {
982           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
983             {
984               next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
985               goto trace00;
986             }
987
988           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
989             {
990               next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
991               goto trace00;
992             }
993         }
994
995       init_nat_k (&kv0, ip0->src_address,
996                   vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
997                   proto0);
998       if (PREDICT_FALSE
999           (clib_bihash_search_8_8
1000            (&sm->per_thread_data[thread_index].in2out, &kv0, &value0) != 0))
1001         {
1002           if (is_slow_path)
1003             {
1004               if (is_output_feature)
1005                 {
1006                   if (PREDICT_FALSE
1007                       (nat_not_translate_output_feature
1008                        (sm, ip0, proto0,
1009                         vnet_buffer (b0)->ip.reass.l4_src_port,
1010                         vnet_buffer (b0)->ip.reass.l4_dst_port,
1011                         thread_index, sw_if_index0)))
1012                     goto trace00;
1013
1014                   /*
1015                    * Send DHCP packets to the ipv4 stack, or we won't
1016                    * be able to use dhcp client on the outside interface
1017                    */
1018                   if (PREDICT_FALSE
1019                       (proto0 == NAT_PROTOCOL_UDP
1020                        && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1021                            clib_host_to_net_u16
1022                            (UDP_DST_PORT_dhcp_to_server))
1023                        && ip0->dst_address.as_u32 == 0xffffffff))
1024                     goto trace00;
1025                 }
1026               else
1027                 {
1028                   if (PREDICT_FALSE
1029                       (snat_not_translate
1030                        (sm, node, sw_if_index0, ip0, proto0,
1031                         rx_fib_index0, thread_index)))
1032                     goto trace00;
1033                 }
1034
1035               next0 = slow_path (sm, b0, ip0,
1036                                  ip0->src_address,
1037                                  vnet_buffer (b0)->ip.reass.l4_src_port,
1038                                  rx_fib_index0,
1039                                  proto0, &s0, node, next0, thread_index, now);
1040               if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1041                 goto trace00;
1042
1043               if (PREDICT_FALSE (!s0))
1044                 goto trace00;
1045             }
1046           else
1047             {
1048               next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1049               goto trace00;
1050             }
1051         }
1052       else
1053         s0 =
1054           pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1055                              value0.value);
1056
1057       b0->flags |= VNET_BUFFER_F_IS_NATED;
1058
1059       old_addr0 = ip0->src_address.as_u32;
1060       ip0->src_address = s0->out2in.addr;
1061       new_addr0 = ip0->src_address.as_u32;
1062       if (!is_output_feature)
1063         vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1064
1065       sum0 = ip0->checksum;
1066       sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1067                              ip4_header_t, src_address /* changed member */ );
1068       ip0->checksum = ip_csum_fold (sum0);
1069
1070
1071       if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1072         {
1073           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1074             {
1075               old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1076               new_port0 = udp0->src_port = s0->out2in.port;
1077               sum0 = tcp0->checksum;
1078               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1079                                      ip4_header_t,
1080                                      dst_address /* changed member */ );
1081               sum0 = ip_csum_update (sum0, old_port0, new_port0,
1082                                      ip4_header_t /* cheat */ ,
1083                                      length /* changed member */ );
1084               mss_clamping (sm->mss_clamping, tcp0, &sum0);
1085               tcp0->checksum = ip_csum_fold (sum0);
1086             }
1087           vlib_increment_simple_counter (is_slow_path ? &sm->
1088                                          counters.slowpath.in2out.tcp : &sm->
1089                                          counters.fastpath.in2out.tcp,
1090                                          thread_index, sw_if_index0, 1);
1091         }
1092       else
1093         {
1094           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1095             {
1096               udp0->src_port = s0->out2in.port;
1097               if (PREDICT_FALSE (udp0->checksum))
1098                 {
1099                   old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1100                   new_port0 = udp0->src_port;
1101                   sum0 = udp0->checksum;
1102                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address  /* changed member */
1103                     );
1104                   sum0 =
1105                     ip_csum_update (sum0, old_port0, new_port0,
1106                                     ip4_header_t /* cheat */ ,
1107                                     length /* changed member */ );
1108                   udp0->checksum = ip_csum_fold (sum0);
1109                 }
1110             }
1111           vlib_increment_simple_counter (is_slow_path ? &sm->
1112                                          counters.slowpath.in2out.udp : &sm->
1113                                          counters.fastpath.in2out.udp,
1114                                          thread_index, sw_if_index0, 1);
1115         }
1116
1117       /* Accounting */
1118       nat44_session_update_counters (s0, now,
1119                                      vlib_buffer_length_in_chain (vm, b0),
1120                                      thread_index);
1121       /* Per-user LRU list maintenance */
1122       nat44_session_update_lru (sm, s0, thread_index);
1123     trace00:
1124
1125       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1126                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1127         {
1128           snat_in2out_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1129           t->is_slow_path = is_slow_path;
1130           t->sw_if_index = sw_if_index0;
1131           t->next_index = next0;
1132           t->session_index = ~0;
1133           if (s0)
1134             t->session_index =
1135               s0 - sm->per_thread_data[thread_index].sessions;
1136         }
1137
1138       if (next0 == SNAT_IN2OUT_NEXT_DROP)
1139         {
1140           vlib_increment_simple_counter (is_slow_path ? &sm->
1141                                          counters.slowpath.in2out.
1142                                          drops : &sm->counters.fastpath.
1143                                          in2out.drops, thread_index,
1144                                          sw_if_index0, 1);
1145         }
1146
1147       if (is_output_feature)
1148         iph_offset1 = vnet_buffer (b1)->ip.reass.save_rewrite_length;
1149
1150       ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1151                               iph_offset1);
1152
1153       udp1 = ip4_next_header (ip1);
1154       tcp1 = (tcp_header_t *) udp1;
1155       icmp1 = (icmp46_header_t *) udp1;
1156
1157       sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1158       rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1159                                sw_if_index1);
1160
1161       if (PREDICT_FALSE (ip1->ttl == 1))
1162         {
1163           vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1164           icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1165                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
1166                                        0);
1167           next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1168           goto trace01;
1169         }
1170
1171       proto1 = ip_proto_to_nat_proto (ip1->protocol);
1172
1173       /* Next configured feature, probably ip4-lookup */
1174       if (is_slow_path)
1175         {
1176           if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1177             {
1178               if (nat_in2out_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
1179                 {
1180                   next1 = SNAT_IN2OUT_NEXT_DROP;
1181                   b1->error =
1182                     node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1183                 }
1184               vlib_increment_simple_counter (is_slow_path ? &sm->
1185                                              counters.slowpath.in2out.
1186                                              other : &sm->counters.fastpath.
1187                                              in2out.other, thread_index,
1188                                              sw_if_index1, 1);
1189               goto trace01;
1190             }
1191
1192           if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1193             {
1194               next1 = icmp_in2out_slow_path
1195                 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1196                  next1, now, thread_index, &s1);
1197               vlib_increment_simple_counter (is_slow_path ? &sm->
1198                                              counters.slowpath.in2out.
1199                                              icmp : &sm->counters.fastpath.
1200                                              in2out.icmp, thread_index,
1201                                              sw_if_index1, 1);
1202               goto trace01;
1203             }
1204         }
1205       else
1206         {
1207           if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1208             {
1209               next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1210               goto trace01;
1211             }
1212
1213           if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1214             {
1215               next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1216               goto trace01;
1217             }
1218         }
1219
1220       init_nat_k (&kv1, ip1->src_address,
1221                   vnet_buffer (b1)->ip.reass.l4_src_port, rx_fib_index1,
1222                   proto1);
1223       if (PREDICT_FALSE
1224           (clib_bihash_search_8_8
1225            (&sm->per_thread_data[thread_index].in2out, &kv1, &value1) != 0))
1226         {
1227           if (is_slow_path)
1228             {
1229               if (is_output_feature)
1230                 {
1231                   if (PREDICT_FALSE
1232                       (nat_not_translate_output_feature
1233                        (sm, ip1, proto1,
1234                         vnet_buffer (b1)->ip.reass.l4_src_port,
1235                         vnet_buffer (b1)->ip.reass.l4_dst_port,
1236                         thread_index, sw_if_index1)))
1237                     goto trace01;
1238
1239                   /*
1240                    * Send DHCP packets to the ipv4 stack, or we won't
1241                    * be able to use dhcp client on the outside interface
1242                    */
1243                   if (PREDICT_FALSE
1244                       (proto1 == NAT_PROTOCOL_UDP
1245                        && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1246                            clib_host_to_net_u16
1247                            (UDP_DST_PORT_dhcp_to_server))
1248                        && ip1->dst_address.as_u32 == 0xffffffff))
1249                     goto trace01;
1250                 }
1251               else
1252                 {
1253                   if (PREDICT_FALSE
1254                       (snat_not_translate
1255                        (sm, node, sw_if_index1, ip1, proto1,
1256                         rx_fib_index1, thread_index)))
1257                     goto trace01;
1258                 }
1259
1260               next1 =
1261                 slow_path (sm, b1, ip1, ip1->src_address,
1262                            vnet_buffer (b1)->ip.reass.l4_src_port,
1263                            rx_fib_index1, proto1, &s1, node, next1,
1264                            thread_index, now);
1265               if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1266                 goto trace01;
1267
1268               if (PREDICT_FALSE (!s1))
1269                 goto trace01;
1270             }
1271           else
1272             {
1273               next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1274               goto trace01;
1275             }
1276         }
1277       else
1278         s1 =
1279           pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1280                              value1.value);
1281
1282       b1->flags |= VNET_BUFFER_F_IS_NATED;
1283
1284       old_addr1 = ip1->src_address.as_u32;
1285       ip1->src_address = s1->out2in.addr;
1286       new_addr1 = ip1->src_address.as_u32;
1287       if (!is_output_feature)
1288         vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1289
1290       sum1 = ip1->checksum;
1291       sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1292                              ip4_header_t, src_address /* changed member */ );
1293       ip1->checksum = ip_csum_fold (sum1);
1294
1295       if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1296         {
1297           if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1298             {
1299               old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1300               new_port1 = udp1->src_port = s1->out2in.port;
1301               sum1 = tcp1->checksum;
1302               sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1303                                      ip4_header_t,
1304                                      dst_address /* changed member */ );
1305               sum1 = ip_csum_update (sum1, old_port1, new_port1,
1306                                      ip4_header_t /* cheat */ ,
1307                                      length /* changed member */ );
1308               mss_clamping (sm->mss_clamping, tcp1, &sum1);
1309               tcp1->checksum = ip_csum_fold (sum1);
1310             }
1311           vlib_increment_simple_counter (is_slow_path ? &sm->
1312                                          counters.slowpath.in2out.tcp : &sm->
1313                                          counters.fastpath.in2out.tcp,
1314                                          thread_index, sw_if_index1, 1);
1315         }
1316       else
1317         {
1318           if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1319             {
1320               udp1->src_port = s1->out2in.port;
1321               if (PREDICT_FALSE (udp1->checksum))
1322                 {
1323                   old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1324                   new_port1 = udp1->src_port;
1325                   sum1 = udp1->checksum;
1326                   sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, dst_address  /* changed member */
1327                     );
1328                   sum1 =
1329                     ip_csum_update (sum1, old_port1, new_port1,
1330                                     ip4_header_t /* cheat */ ,
1331                                     length /* changed member */ );
1332                   udp1->checksum = ip_csum_fold (sum1);
1333                 }
1334             }
1335           vlib_increment_simple_counter (is_slow_path ? &sm->
1336                                          counters.slowpath.in2out.udp : &sm->
1337                                          counters.fastpath.in2out.udp,
1338                                          thread_index, sw_if_index1, 1);
1339         }
1340
1341       /* Accounting */
1342       nat44_session_update_counters (s1, now,
1343                                      vlib_buffer_length_in_chain (vm, b1),
1344                                      thread_index);
1345       /* Per-user LRU list maintenance */
1346       nat44_session_update_lru (sm, s1, thread_index);
1347     trace01:
1348
1349       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1350                          && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1351         {
1352           snat_in2out_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
1353           t->sw_if_index = sw_if_index1;
1354           t->next_index = next1;
1355           t->session_index = ~0;
1356           if (s1)
1357             t->session_index =
1358               s1 - sm->per_thread_data[thread_index].sessions;
1359         }
1360
1361       if (next1 == SNAT_IN2OUT_NEXT_DROP)
1362         {
1363           vlib_increment_simple_counter (is_slow_path ? &sm->
1364                                          counters.slowpath.in2out.
1365                                          drops : &sm->counters.fastpath.
1366                                          in2out.drops, thread_index,
1367                                          sw_if_index1, 1);
1368         }
1369
1370       n_left_from -= 2;
1371       next[0] = next0;
1372       next[1] = next1;
1373       next += 2;
1374     }
1375
1376   while (n_left_from > 0)
1377     {
1378       vlib_buffer_t *b0;
1379       u32 next0;
1380       u32 sw_if_index0;
1381       ip4_header_t *ip0;
1382       ip_csum_t sum0;
1383       u32 new_addr0, old_addr0;
1384       u16 old_port0, new_port0;
1385       udp_header_t *udp0;
1386       tcp_header_t *tcp0;
1387       icmp46_header_t *icmp0;
1388       u32 rx_fib_index0;
1389       u32 proto0;
1390       snat_session_t *s0 = 0;
1391       clib_bihash_kv_8_8_t kv0, value0;
1392       u32 iph_offset0 = 0;
1393
1394       b0 = *b;
1395       b++;
1396       next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1397
1398       if (is_output_feature)
1399         iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1400
1401       ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1402                               iph_offset0);
1403
1404       udp0 = ip4_next_header (ip0);
1405       tcp0 = (tcp_header_t *) udp0;
1406       icmp0 = (icmp46_header_t *) udp0;
1407
1408       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1409       rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1410                                sw_if_index0);
1411
1412       if (PREDICT_FALSE (ip0->ttl == 1))
1413         {
1414           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1415           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1416                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
1417                                        0);
1418           next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1419           goto trace0;
1420         }
1421
1422       proto0 = ip_proto_to_nat_proto (ip0->protocol);
1423
1424       /* Next configured feature, probably ip4-lookup */
1425       if (is_slow_path)
1426         {
1427           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1428             {
1429               if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1430                 {
1431                   next0 = SNAT_IN2OUT_NEXT_DROP;
1432                   b0->error =
1433                     node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1434                 }
1435               vlib_increment_simple_counter (is_slow_path ? &sm->
1436                                              counters.slowpath.in2out.
1437                                              other : &sm->counters.fastpath.
1438                                              in2out.other, thread_index,
1439                                              sw_if_index0, 1);
1440               goto trace0;
1441             }
1442
1443           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1444             {
1445               next0 = icmp_in2out_slow_path
1446                 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1447                  next0, now, thread_index, &s0);
1448               vlib_increment_simple_counter (is_slow_path ? &sm->
1449                                              counters.slowpath.in2out.
1450                                              icmp : &sm->counters.fastpath.
1451                                              in2out.icmp, thread_index,
1452                                              sw_if_index0, 1);
1453               goto trace0;
1454             }
1455         }
1456       else
1457         {
1458           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1459             {
1460               next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1461               goto trace0;
1462             }
1463
1464           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1465             {
1466               next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1467               goto trace0;
1468             }
1469         }
1470
1471       init_nat_k (&kv0, ip0->src_address,
1472                   vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1473                   proto0);
1474
1475       if (clib_bihash_search_8_8
1476           (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1477         {
1478           if (is_slow_path)
1479             {
1480               if (is_output_feature)
1481                 {
1482                   if (PREDICT_FALSE
1483                       (nat_not_translate_output_feature
1484                        (sm, ip0, proto0,
1485                         vnet_buffer (b0)->ip.reass.l4_src_port,
1486                         vnet_buffer (b0)->ip.reass.l4_dst_port,
1487                         thread_index, sw_if_index0)))
1488                     goto trace0;
1489
1490                   /*
1491                    * Send DHCP packets to the ipv4 stack, or we won't
1492                    * be able to use dhcp client on the outside interface
1493                    */
1494                   if (PREDICT_FALSE
1495                       (proto0 == NAT_PROTOCOL_UDP
1496                        && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1497                            clib_host_to_net_u16
1498                            (UDP_DST_PORT_dhcp_to_server))
1499                        && ip0->dst_address.as_u32 == 0xffffffff))
1500                     goto trace0;
1501                 }
1502               else
1503                 {
1504                   if (PREDICT_FALSE
1505                       (snat_not_translate
1506                        (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1507                         thread_index)))
1508                     goto trace0;
1509                 }
1510
1511               next0 =
1512                 slow_path (sm, b0, ip0, ip0->src_address,
1513                            vnet_buffer (b0)->ip.reass.l4_src_port,
1514                            rx_fib_index0, proto0, &s0, node, next0,
1515                            thread_index, now);
1516
1517               if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1518                 goto trace0;
1519
1520               if (PREDICT_FALSE (!s0))
1521                 goto trace0;
1522             }
1523           else
1524             {
1525               next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1526               goto trace0;
1527             }
1528         }
1529       else
1530         s0 =
1531           pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1532                              value0.value);
1533
1534       b0->flags |= VNET_BUFFER_F_IS_NATED;
1535
1536       old_addr0 = ip0->src_address.as_u32;
1537       ip0->src_address = s0->out2in.addr;
1538       new_addr0 = ip0->src_address.as_u32;
1539       if (!is_output_feature)
1540         vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1541
1542       sum0 = ip0->checksum;
1543       sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1544                              ip4_header_t, src_address /* changed member */ );
1545       ip0->checksum = ip_csum_fold (sum0);
1546
1547       if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1548         {
1549           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1550             {
1551               old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1552               new_port0 = udp0->src_port = s0->out2in.port;
1553               sum0 = tcp0->checksum;
1554               sum0 =
1555                 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1556                                 dst_address /* changed member */ );
1557               sum0 =
1558                 ip_csum_update (sum0, old_port0, new_port0,
1559                                 ip4_header_t /* cheat */ ,
1560                                 length /* changed member */ );
1561               mss_clamping (sm->mss_clamping, tcp0, &sum0);
1562               tcp0->checksum = ip_csum_fold (sum0);
1563             }
1564           vlib_increment_simple_counter (is_slow_path ? &sm->
1565                                          counters.slowpath.in2out.tcp : &sm->
1566                                          counters.fastpath.in2out.tcp,
1567                                          thread_index, sw_if_index0, 1);
1568         }
1569       else
1570         {
1571           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1572             {
1573               udp0->src_port = s0->out2in.port;
1574               if (PREDICT_FALSE (udp0->checksum))
1575                 {
1576                   old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1577                   new_port0 = udp0->src_port;
1578                   sum0 = udp0->checksum;
1579                   sum0 =
1580                     ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1581                                     dst_address /* changed member */ );
1582                   sum0 =
1583                     ip_csum_update (sum0, old_port0, new_port0,
1584                                     ip4_header_t /* cheat */ ,
1585                                     length /* changed member */ );
1586                   udp0->checksum = ip_csum_fold (sum0);
1587                 }
1588             }
1589           vlib_increment_simple_counter (is_slow_path ? &sm->
1590                                          counters.slowpath.in2out.udp : &sm->
1591                                          counters.fastpath.in2out.udp,
1592                                          thread_index, sw_if_index0, 1);
1593         }
1594
1595       /* Accounting */
1596       nat44_session_update_counters (s0, now,
1597                                      vlib_buffer_length_in_chain (vm, b0),
1598                                      thread_index);
1599       /* Per-user LRU list maintenance */
1600       nat44_session_update_lru (sm, s0, thread_index);
1601
1602     trace0:
1603       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1604                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1605         {
1606           snat_in2out_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1607           t->is_slow_path = is_slow_path;
1608           t->sw_if_index = sw_if_index0;
1609           t->next_index = next0;
1610           t->session_index = ~0;
1611           if (s0)
1612             t->session_index =
1613               s0 - sm->per_thread_data[thread_index].sessions;
1614         }
1615
1616       if (next0 == SNAT_IN2OUT_NEXT_DROP)
1617         {
1618           vlib_increment_simple_counter (is_slow_path ? &sm->
1619                                          counters.slowpath.in2out.
1620                                          drops : &sm->counters.fastpath.
1621                                          in2out.drops, thread_index,
1622                                          sw_if_index0, 1);
1623         }
1624
1625       n_left_from--;
1626       next[0] = next0;
1627       next++;
1628     }
1629
1630   vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1631                                frame->n_vectors);
1632   return frame->n_vectors;
1633 }
1634
1635 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1636                                  vlib_node_runtime_t * node,
1637                                  vlib_frame_t * frame)
1638 {
1639   return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1640                                      0);
1641 }
1642
1643 /* *INDENT-OFF* */
1644 VLIB_REGISTER_NODE (snat_in2out_node) = {
1645   .name = "nat44-in2out",
1646   .vector_size = sizeof (u32),
1647   .format_trace = format_snat_in2out_trace,
1648   .type = VLIB_NODE_TYPE_INTERNAL,
1649
1650   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1651   .error_strings = snat_in2out_error_strings,
1652
1653   .runtime_data_bytes = sizeof (snat_runtime_t),
1654
1655   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1656
1657   /* edit / add dispositions here */
1658   .next_nodes = {
1659     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1660     [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1661     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1662     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1663   },
1664 };
1665 /* *INDENT-ON* */
1666
1667 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1668                                         vlib_node_runtime_t * node,
1669                                         vlib_frame_t * frame)
1670 {
1671   return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1672                                      1);
1673 }
1674
1675 /* *INDENT-OFF* */
1676 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1677   .name = "nat44-in2out-output",
1678   .vector_size = sizeof (u32),
1679   .format_trace = format_snat_in2out_trace,
1680   .type = VLIB_NODE_TYPE_INTERNAL,
1681
1682   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1683   .error_strings = snat_in2out_error_strings,
1684
1685   .runtime_data_bytes = sizeof (snat_runtime_t),
1686
1687   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1688
1689   /* edit / add dispositions here */
1690   .next_nodes = {
1691     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1692     [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1693     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1694     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1695   },
1696 };
1697 /* *INDENT-ON* */
1698
1699 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1700                                           vlib_node_runtime_t * node,
1701                                           vlib_frame_t * frame)
1702 {
1703   return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1704                                      0);
1705 }
1706
1707 /* *INDENT-OFF* */
1708 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1709   .name = "nat44-in2out-slowpath",
1710   .vector_size = sizeof (u32),
1711   .format_trace = format_snat_in2out_trace,
1712   .type = VLIB_NODE_TYPE_INTERNAL,
1713
1714   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1715   .error_strings = snat_in2out_error_strings,
1716
1717   .runtime_data_bytes = sizeof (snat_runtime_t),
1718
1719   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1720
1721   /* edit / add dispositions here */
1722   .next_nodes = {
1723     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1724     [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1725     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1726     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1727   },
1728 };
1729 /* *INDENT-ON* */
1730
1731 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1732                                                  vlib_node_runtime_t * node,
1733                                                  vlib_frame_t * frame)
1734 {
1735   return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1736                                      1);
1737 }
1738
1739 /* *INDENT-OFF* */
1740 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1741   .name = "nat44-in2out-output-slowpath",
1742   .vector_size = sizeof (u32),
1743   .format_trace = format_snat_in2out_trace,
1744   .type = VLIB_NODE_TYPE_INTERNAL,
1745
1746   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1747   .error_strings = snat_in2out_error_strings,
1748
1749   .runtime_data_bytes = sizeof (snat_runtime_t),
1750
1751   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1752
1753   /* edit / add dispositions here */
1754   .next_nodes = {
1755     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1756     [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1757     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1758     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1759   },
1760 };
1761 /* *INDENT-ON* */
1762
1763 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
1764                                       vlib_node_runtime_t * node,
1765                                       vlib_frame_t * frame)
1766 {
1767   u32 n_left_from, *from, *to_next;
1768   snat_in2out_next_t next_index;
1769   snat_main_t *sm = &snat_main;
1770   int is_hairpinning = 0;
1771
1772   from = vlib_frame_vector_args (frame);
1773   n_left_from = frame->n_vectors;
1774   next_index = node->cached_next_index;
1775
1776   while (n_left_from > 0)
1777     {
1778       u32 n_left_to_next;
1779
1780       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1781
1782       while (n_left_from > 0 && n_left_to_next > 0)
1783         {
1784           u32 bi0;
1785           vlib_buffer_t *b0;
1786           u32 next0;
1787           u32 sw_if_index0;
1788           ip4_header_t *ip0;
1789           ip_csum_t sum0;
1790           u32 new_addr0, old_addr0;
1791           u16 old_port0, new_port0;
1792           udp_header_t *udp0;
1793           tcp_header_t *tcp0;
1794           icmp46_header_t *icmp0;
1795           u32 proto0;
1796           u32 rx_fib_index0;
1797           ip4_address_t sm0_addr;
1798           u16 sm0_port;
1799           u32 sm0_fib_index;
1800
1801
1802           /* speculatively enqueue b0 to the current next frame */
1803           bi0 = from[0];
1804           to_next[0] = bi0;
1805           from += 1;
1806           to_next += 1;
1807           n_left_from -= 1;
1808           n_left_to_next -= 1;
1809
1810           b0 = vlib_get_buffer (vm, bi0);
1811           next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1812
1813           ip0 = vlib_buffer_get_current (b0);
1814           udp0 = ip4_next_header (ip0);
1815           tcp0 = (tcp_header_t *) udp0;
1816           icmp0 = (icmp46_header_t *) udp0;
1817
1818           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1819           rx_fib_index0 =
1820             ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1821
1822           if (PREDICT_FALSE (ip0->ttl == 1))
1823             {
1824               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1825               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1826                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1827                                            0);
1828               next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1829               goto trace0;
1830             }
1831
1832           proto0 = ip_proto_to_nat_proto (ip0->protocol);
1833
1834           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1835             goto trace0;
1836
1837           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1838             {
1839               next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
1840                                    rx_fib_index0, node, next0, ~0, 0, 0);
1841               goto trace0;
1842             }
1843
1844           if (snat_static_mapping_match
1845               (sm, ip0->src_address, udp0->src_port, rx_fib_index0, proto0,
1846                &sm0_addr, &sm0_port, &sm0_fib_index, 0, 0, 0, 0, 0, 0))
1847             {
1848               b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1849               next0 = SNAT_IN2OUT_NEXT_DROP;
1850               goto trace0;
1851             }
1852
1853           new_addr0 = sm0_addr.as_u32;
1854           new_port0 = sm0_port;
1855           vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0_fib_index;
1856           old_addr0 = ip0->src_address.as_u32;
1857           ip0->src_address.as_u32 = new_addr0;
1858
1859           sum0 = ip0->checksum;
1860           sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1861                                  ip4_header_t,
1862                                  src_address /* changed member */ );
1863           ip0->checksum = ip_csum_fold (sum0);
1864
1865           if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1866             {
1867               old_port0 = udp0->src_port;
1868               udp0->src_port = new_port0;
1869
1870               if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1871                 {
1872                   sum0 = tcp0->checksum;
1873                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1874                                          ip4_header_t,
1875                                          dst_address /* changed member */ );
1876                   sum0 = ip_csum_update (sum0, old_port0, new_port0,
1877                                          ip4_header_t /* cheat */ ,
1878                                          length /* changed member */ );
1879                   mss_clamping (sm->mss_clamping, tcp0, &sum0);
1880                   tcp0->checksum = ip_csum_fold (sum0);
1881                 }
1882               else if (udp0->checksum)
1883                 {
1884                   sum0 = udp0->checksum;
1885                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1886                                          ip4_header_t,
1887                                          dst_address /* changed member */ );
1888                   sum0 = ip_csum_update (sum0, old_port0, new_port0,
1889                                          ip4_header_t /* cheat */ ,
1890                                          length /* changed member */ );
1891                   udp0->checksum = ip_csum_fold (sum0);
1892                 }
1893             }
1894           else
1895             {
1896               if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1897                 {
1898                   sum0 = tcp0->checksum;
1899                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1900                                          ip4_header_t,
1901                                          dst_address /* changed member */ );
1902                   mss_clamping (sm->mss_clamping, tcp0, &sum0);
1903                   tcp0->checksum = ip_csum_fold (sum0);
1904                 }
1905               else if (udp0->checksum)
1906                 {
1907                   sum0 = udp0->checksum;
1908                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1909                                          ip4_header_t,
1910                                          dst_address /* changed member */ );
1911                   udp0->checksum = ip_csum_fold (sum0);
1912                 }
1913             }
1914
1915           /* Hairpinning */
1916           snat_hairpinning (vm, node, sm, b0, ip0, udp0, tcp0, proto0, 0,
1917                             0 /* do_trace */ );
1918           is_hairpinning = 1;
1919
1920         trace0:
1921           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1922                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1923             {
1924               snat_in2out_trace_t *t =
1925                 vlib_add_trace (vm, node, b0, sizeof (*t));
1926               t->sw_if_index = sw_if_index0;
1927               t->next_index = next0;
1928               t->is_hairpinning = is_hairpinning;
1929             }
1930
1931           if (next0 != SNAT_IN2OUT_NEXT_DROP)
1932             {
1933
1934               vlib_increment_simple_counter (&sm->counters.fastpath.
1935                                              in2out.other, sw_if_index0,
1936                                              vm->thread_index, 1);
1937             }
1938
1939           /* verify speculative enqueue, maybe switch current next frame */
1940           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1941                                            to_next, n_left_to_next,
1942                                            bi0, next0);
1943         }
1944
1945       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1946     }
1947
1948   return frame->n_vectors;
1949 }
1950
1951
1952 /* *INDENT-OFF* */
1953 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1954   .name = "nat44-in2out-fast",
1955   .vector_size = sizeof (u32),
1956   .format_trace = format_snat_in2out_fast_trace,
1957   .type = VLIB_NODE_TYPE_INTERNAL,
1958
1959   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1960   .error_strings = snat_in2out_error_strings,
1961
1962   .runtime_data_bytes = sizeof (snat_runtime_t),
1963
1964   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1965
1966   /* edit / add dispositions here */
1967   .next_nodes = {
1968     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1969     [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1970     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1971     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1972   },
1973 };
1974 /* *INDENT-ON* */
1975
1976 /*
1977  * fd.io coding-style-patch-verification: ON
1978  *
1979  * Local Variables:
1980  * eval: (c-set-style "gnu")
1981  * End:
1982  */