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