ca3c272a26c62587ede71270b673c7b392fe9e50
[vpp.git] / src / plugins / nat / nat44-ei / nat44_ei_out2in.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 endpoint-dependent outside to inside network translation
18  */
19
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22
23 #include <vnet/ip/ip.h>
24 #include <vnet/udp/udp_local.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/fib/ip4_fib.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
38 typedef struct
39 {
40   u32 sw_if_index;
41   u32 next_index;
42   u32 session_index;
43 } snat_out2in_trace_t;
44
45 /* packet trace format function */
46 static u8 *
47 format_snat_out2in_trace (u8 * s, va_list * args)
48 {
49   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
50   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
51   snat_out2in_trace_t *t = va_arg (*args, snat_out2in_trace_t *);
52
53   s =
54     format (s,
55             "NAT44_OUT2IN: sw_if_index %d, next index %d, session index %d",
56             t->sw_if_index, t->next_index, t->session_index);
57   return s;
58 }
59
60 static u8 *
61 format_snat_out2in_fast_trace (u8 * s, va_list * args)
62 {
63   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
64   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
65   snat_out2in_trace_t *t = va_arg (*args, snat_out2in_trace_t *);
66
67   s = format (s, "NAT44_OUT2IN_FAST: sw_if_index %d, next index %d",
68               t->sw_if_index, t->next_index);
69   return s;
70 }
71
72 #define foreach_snat_out2in_error                       \
73 _(UNSUPPORTED_PROTOCOL, "unsupported protocol")         \
74 _(OUT_OF_PORTS, "out of ports")                         \
75 _(BAD_ICMP_TYPE, "unsupported ICMP type")               \
76 _(NO_TRANSLATION, "no translation")                     \
77 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")   \
78 _(CANNOT_CREATE_USER, "cannot create NAT user")
79
80 typedef enum
81 {
82 #define _(sym,str) SNAT_OUT2IN_ERROR_##sym,
83   foreach_snat_out2in_error
84 #undef _
85     SNAT_OUT2IN_N_ERROR,
86 } snat_out2in_error_t;
87
88 static char *snat_out2in_error_strings[] = {
89 #define _(sym,string) string,
90   foreach_snat_out2in_error
91 #undef _
92 };
93
94 typedef enum
95 {
96   SNAT_OUT2IN_NEXT_DROP,
97   SNAT_OUT2IN_NEXT_LOOKUP,
98   SNAT_OUT2IN_NEXT_ICMP_ERROR,
99   SNAT_OUT2IN_N_NEXT,
100 } snat_out2in_next_t;
101
102 #ifndef CLIB_MARCH_VARIANT
103 int
104 nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
105 {
106   snat_main_t *sm = &snat_main;
107   nat44_is_idle_session_ctx_t *ctx = arg;
108   snat_session_t *s;
109   u64 sess_timeout_time;
110   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
111                                                        ctx->thread_index);
112   clib_bihash_kv_8_8_t s_kv;
113
114   s = pool_elt_at_index (tsm->sessions, kv->value);
115   sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
116   if (ctx->now >= sess_timeout_time)
117     {
118       init_nat_i2o_k (&s_kv, s);
119       if (clib_bihash_add_del_8_8 (&sm->in2out, &s_kv, 0))
120         nat_elog_warn ("out2in key del failed");
121
122       nat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
123                                           s->in2out.addr.as_u32,
124                                           s->out2in.addr.as_u32,
125                                           s->nat_proto,
126                                           s->in2out.port,
127                                           s->out2in.port,
128                                           s->in2out.fib_index);
129
130       nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
131                                &s->in2out.addr, s->in2out.port,
132                                &s->out2in.addr, s->out2in.port, s->nat_proto);
133
134       nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
135                    s->ext_host_port, s->nat_proto, s->out2in.fib_index,
136                    ctx->thread_index);
137
138       if (!snat_is_session_static (s))
139         snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
140                                             &s->out2in.addr, s->out2in.port,
141                                             s->nat_proto);
142
143       nat44_delete_session (sm, s, ctx->thread_index);
144       return 1;
145     }
146
147   return 0;
148 }
149 #endif
150
151 /**
152  * @brief Create session for static mapping.
153  *
154  * Create NAT session initiated by host from external network with static
155  * mapping.
156  *
157  * @param sm     NAT main.
158  * @param b0     Vlib buffer.
159  * @param in2out In2out NAT44 session key.
160  * @param out2in Out2in NAT44 session key.
161  * @param node   Vlib node.
162  *
163  * @returns SNAT session if successfully created otherwise 0.
164  */
165 static inline snat_session_t *
166 create_session_for_static_mapping (snat_main_t * sm,
167                                    vlib_buffer_t * b0,
168                                    ip4_address_t i2o_addr,
169                                    u16 i2o_port,
170                                    u32 i2o_fib_index,
171                                    ip4_address_t o2i_addr,
172                                    u16 o2i_port,
173                                    u32 o2i_fib_index,
174                                    nat_protocol_t proto,
175                                    vlib_node_runtime_t * node,
176                                    u32 thread_index, f64 now)
177 {
178   snat_user_t *u;
179   snat_session_t *s;
180   clib_bihash_kv_8_8_t kv0;
181   ip4_header_t *ip0;
182   udp_header_t *udp0;
183   nat44_is_idle_session_ctx_t ctx0;
184
185   if (PREDICT_FALSE (nat44_ei_maximum_sessions_exceeded (sm, thread_index)))
186     {
187       b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_SESSIONS_EXCEEDED];
188       nat_elog_notice ("maximum sessions exceeded");
189       return 0;
190     }
191
192   ip0 = vlib_buffer_get_current (b0);
193   udp0 = ip4_next_header (ip0);
194
195   u = nat_user_get_or_create (sm, &i2o_addr, i2o_fib_index, thread_index);
196   if (!u)
197     {
198       b0->error = node->errors[SNAT_OUT2IN_ERROR_CANNOT_CREATE_USER];
199       return 0;
200     }
201
202   s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
203   if (!s)
204     {
205       nat44_delete_user_with_no_session (sm, u, thread_index);
206       nat_elog_warn ("create NAT session failed");
207       return 0;
208     }
209
210   s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
211   s->ext_host_addr.as_u32 = ip0->src_address.as_u32;
212   s->ext_host_port = udp0->src_port;
213   user_session_increment (sm, u, 1 /* static */ );
214   s->in2out.addr = i2o_addr;
215   s->in2out.port = i2o_port;
216   s->in2out.fib_index = i2o_fib_index;
217   s->out2in.addr = o2i_addr;
218   s->out2in.port = o2i_port;
219   s->out2in.fib_index = o2i_fib_index;
220   s->nat_proto = proto;
221
222   /* Add to translation hashes */
223   ctx0.now = now;
224   ctx0.thread_index = thread_index;
225   init_nat_i2o_kv (&kv0, s, thread_index,
226                    s - sm->per_thread_data[thread_index].sessions);
227   if (clib_bihash_add_or_overwrite_stale_8_8 (
228         &sm->in2out, &kv0, nat44_i2o_is_idle_session_cb, &ctx0))
229     nat_elog_notice ("in2out key add failed");
230
231   init_nat_o2i_kv (&kv0, s, thread_index,
232                    s - sm->per_thread_data[thread_index].sessions);
233   if (clib_bihash_add_or_overwrite_stale_8_8 (
234         &sm->out2in, &kv0, nat44_o2i_is_idle_session_cb, &ctx0))
235     nat_elog_notice ("out2in key add failed");
236
237   /* log NAT event */
238   nat_ipfix_logging_nat44_ses_create (thread_index,
239                                       s->in2out.addr.as_u32,
240                                       s->out2in.addr.as_u32,
241                                       s->nat_proto,
242                                       s->in2out.port,
243                                       s->out2in.port, s->in2out.fib_index);
244
245   nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
246                            &s->in2out.addr, s->in2out.port, &s->out2in.addr,
247                            s->out2in.port, s->nat_proto);
248
249   nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
250                s->out2in.port, &s->ext_host_addr, s->ext_host_port,
251                &s->ext_host_nat_addr, s->ext_host_nat_port,
252                s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
253
254   return s;
255 }
256
257 #ifndef CLIB_MARCH_VARIANT
258 static_always_inline snat_out2in_error_t
259 icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
260               ip4_address_t * addr, u16 * port, nat_protocol_t * nat_proto)
261 {
262   icmp46_header_t *icmp0;
263   icmp_echo_header_t *echo0, *inner_echo0 = 0;
264   ip4_header_t *inner_ip0;
265   void *l4_header = 0;
266   icmp46_header_t *inner_icmp0;
267
268   icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
269   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
270
271   if (!icmp_type_is_error_message
272       (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
273     {
274       *nat_proto = NAT_PROTOCOL_ICMP;
275       *addr = ip0->dst_address;
276       *port = vnet_buffer (b)->ip.reass.l4_src_port;
277     }
278   else
279     {
280       inner_ip0 = (ip4_header_t *) (echo0 + 1);
281       l4_header = ip4_next_header (inner_ip0);
282       *nat_proto = ip_proto_to_nat_proto (inner_ip0->protocol);
283       *addr = inner_ip0->src_address;
284       switch (*nat_proto)
285         {
286         case NAT_PROTOCOL_ICMP:
287           inner_icmp0 = (icmp46_header_t *) l4_header;
288           inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
289           *port = inner_echo0->identifier;
290           break;
291         case NAT_PROTOCOL_UDP:
292         case NAT_PROTOCOL_TCP:
293           *port = ((tcp_udp_header_t *) l4_header)->src_port;
294           break;
295         default:
296           return SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL;
297         }
298     }
299   return -1;                    /* success */
300 }
301
302 /**
303  * Get address and port values to be used for ICMP packet translation
304  * and create session if needed
305  *
306  * @param[in,out] sm             NAT main
307  * @param[in,out] node           NAT node runtime
308  * @param[in] thread_index       thread index
309  * @param[in,out] b0             buffer containing packet to be translated
310  * @param[in,out] ip0            ip header
311  * @param[out] p_proto           protocol used for matching
312  * @param[out] p_value           address and port after NAT translation
313  * @param[out] p_dont_translate  if packet should not be translated
314  * @param d                      optional parameter
315  * @param e                      optional parameter
316  */
317 u32
318 icmp_match_out2in_slow (snat_main_t *sm, vlib_node_runtime_t *node,
319                         u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0,
320                         ip4_address_t *addr, u16 *port, u32 *fib_index,
321                         nat_protocol_t *proto, snat_session_t **p_s0,
322                         u8 *dont_translate)
323 {
324   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
325   u32 sw_if_index0;
326   snat_session_t *s0 = 0;
327   clib_bihash_kv_8_8_t kv0, value0;
328   u8 is_addr_only;
329   u32 next0 = ~0;
330   int err;
331   u8 identity_nat;
332   vlib_main_t *vm = vlib_get_main ();
333   *dont_translate = 0;
334
335   sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
336   *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
337
338   *proto = 0;
339
340   err = icmp_get_key (b0, ip0, addr, port, proto);
341   if (err != -1)
342     {
343       b0->error = node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
344       next0 = SNAT_OUT2IN_NEXT_DROP;
345       goto out;
346     }
347
348   ip4_address_t mapping_addr;
349   u16 mapping_port;
350   u32 mapping_fib_index;
351
352   init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
353   if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
354     {
355       /* Try to match static mapping by external address and port,
356          destination address and port in packet */
357       if (nat44_ei_static_mapping_match (
358             *addr, *port, *fib_index, *proto, &mapping_addr, &mapping_port,
359             &mapping_fib_index, 1, &is_addr_only, &identity_nat))
360         {
361           if (!sm->forwarding_enabled)
362             {
363               /* Don't NAT packet aimed at the intfc address */
364               if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
365                                                     ip0->dst_address.as_u32)))
366                 {
367                   *dont_translate = 1;
368                   goto out;
369                 }
370               b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
371               next0 = SNAT_OUT2IN_NEXT_DROP;
372               goto out;
373             }
374           else
375             {
376               *dont_translate = 1;
377               goto out;
378             }
379         }
380
381       if (PREDICT_FALSE
382           (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
383            ICMP4_echo_reply
384            && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
385                ICMP4_echo_request || !is_addr_only)))
386         {
387           b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
388           next0 = SNAT_OUT2IN_NEXT_DROP;
389           goto out;
390         }
391
392       if (PREDICT_FALSE (identity_nat))
393         {
394           *dont_translate = 1;
395           goto out;
396         }
397       /* Create session initiated by host from external network */
398       s0 =
399         create_session_for_static_mapping (sm, b0, mapping_addr, mapping_port,
400                                            mapping_fib_index, *addr, *port,
401                                            *fib_index, *proto, node,
402                                            thread_index, vlib_time_now (vm));
403
404       if (!s0)
405         {
406           next0 = SNAT_OUT2IN_NEXT_DROP;
407           goto out;
408         }
409     }
410   else
411     {
412       if (PREDICT_FALSE
413           (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
414            ICMP4_echo_reply
415            && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
416            ICMP4_echo_request
417            && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
418                                            reass.icmp_type_or_tcp_flags)))
419         {
420           b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
421           next0 = SNAT_OUT2IN_NEXT_DROP;
422           goto out;
423         }
424
425       s0 = pool_elt_at_index (tsm->sessions,
426                               nat_value_get_session_index (&value0));
427     }
428
429 out:
430   if (s0)
431     {
432       *addr = s0->in2out.addr;
433       *port = s0->in2out.port;
434       *fib_index = s0->in2out.fib_index;
435     }
436   if (p_s0)
437     *p_s0 = s0;
438   return next0;
439 }
440 #endif
441
442 #ifndef CLIB_MARCH_VARIANT
443 /**
444  * Get address and port values to be used for ICMP packet translation
445  *
446  * @param[in] sm                 NAT main
447  * @param[in,out] node           NAT node runtime
448  * @param[in] thread_index       thread index
449  * @param[in,out] b0             buffer containing packet to be translated
450  * @param[in,out] ip0            ip header
451  * @param[out] p_proto           protocol used for matching
452  * @param[out] p_value           address and port after NAT translation
453  * @param[out] p_dont_translate  if packet should not be translated
454  * @param d                      optional parameter
455  * @param e                      optional parameter
456  */
457 u32
458 icmp_match_out2in_fast (snat_main_t *sm, vlib_node_runtime_t *node,
459                         u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0,
460                         ip4_address_t *mapping_addr, u16 *mapping_port,
461                         u32 *mapping_fib_index, nat_protocol_t *proto,
462                         snat_session_t **p_s0, u8 *dont_translate)
463 {
464   u32 sw_if_index0;
465   u32 rx_fib_index0;
466   u8 is_addr_only;
467   u32 next0 = ~0;
468   int err;
469   ip4_address_t addr;
470   u16 port;
471   *dont_translate = 0;
472
473   sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
474   rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
475
476   err = icmp_get_key (b0, ip0, &addr, &port, proto);
477   if (err != -1)
478     {
479       b0->error = node->errors[err];
480       next0 = SNAT_OUT2IN_NEXT_DROP;
481       goto out;
482     }
483   if (nat44_ei_static_mapping_match (addr, port, rx_fib_index0, *proto,
484                                      mapping_addr, mapping_port,
485                                      mapping_fib_index, 1, &is_addr_only, 0))
486     {
487       /* Don't NAT packet aimed at the intfc address */
488       if (is_interface_addr (sm, node, sw_if_index0, ip0->dst_address.as_u32))
489         {
490           *dont_translate = 1;
491           goto out;
492         }
493       b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
494       next0 = SNAT_OUT2IN_NEXT_DROP;
495       goto out;
496     }
497
498   if (PREDICT_FALSE
499       (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_reply
500        && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
501            ICMP4_echo_request || !is_addr_only)
502        && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
503                                        reass.icmp_type_or_tcp_flags)))
504     {
505       b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
506       next0 = SNAT_OUT2IN_NEXT_DROP;
507       goto out;
508     }
509
510 out:
511   return next0;
512 }
513 #endif
514
515 u32 icmp_out2in (snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0,
516                  icmp46_header_t *icmp0, u32 sw_if_index0, u32 rx_fib_index0,
517                  vlib_node_runtime_t *node, u32 next0, u32 thread_index,
518                  snat_session_t **p_s0);
519
520 #ifndef CLIB_MARCH_VARIANT
521 u32
522 icmp_out2in (snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0,
523              icmp46_header_t *icmp0, u32 sw_if_index0, u32 rx_fib_index0,
524              vlib_node_runtime_t *node, u32 next0, u32 thread_index,
525              snat_session_t **p_s0)
526 {
527   icmp_echo_header_t *echo0, *inner_echo0 = 0;
528   ip4_header_t *inner_ip0 = 0;
529   void *l4_header = 0;
530   icmp46_header_t *inner_icmp0;
531   u8 dont_translate;
532   u32 new_addr0, old_addr0;
533   u16 old_id0, new_id0;
534   ip_csum_t sum0;
535   u16 checksum0;
536   u32 next0_tmp;
537   vlib_main_t *vm = vlib_get_main ();
538   ip4_address_t addr;
539   u16 port;
540   u32 fib_index;
541   nat_protocol_t proto;
542
543   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
544
545   next0_tmp =
546     sm->icmp_match_out2in_cb (sm, node, thread_index, b0, ip0, &addr, &port,
547                               &fib_index, &proto, p_s0, &dont_translate);
548   if (next0_tmp != ~0)
549     next0 = next0_tmp;
550   if (next0 == SNAT_OUT2IN_NEXT_DROP || dont_translate)
551     goto out;
552
553   if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
554     {
555       sum0 =
556         ip_incremental_checksum_buffer (vm, b0,
557                                         (u8 *) icmp0 -
558                                         (u8 *) vlib_buffer_get_current (b0),
559                                         ntohs (ip0->length) -
560                                         ip4_header_bytes (ip0), 0);
561       checksum0 = ~ip_csum_fold (sum0);
562       if (checksum0 != 0 && checksum0 != 0xffff)
563         {
564           next0 = SNAT_OUT2IN_NEXT_DROP;
565           goto out;
566         }
567     }
568
569   old_addr0 = ip0->dst_address.as_u32;
570   new_addr0 = ip0->dst_address.as_u32 = addr.as_u32;
571   vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
572
573   sum0 = ip0->checksum;
574   sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
575                          dst_address /* changed member */ );
576   ip0->checksum = ip_csum_fold (sum0);
577
578
579   if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
580     {
581       if (icmp0->checksum == 0)
582         icmp0->checksum = 0xffff;
583
584       if (!icmp_type_is_error_message (icmp0->type))
585         {
586           new_id0 = port;
587           if (PREDICT_FALSE (new_id0 != echo0->identifier))
588             {
589               old_id0 = echo0->identifier;
590               new_id0 = port;
591               echo0->identifier = new_id0;
592
593               sum0 = icmp0->checksum;
594               sum0 =
595                 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
596                                 identifier /* changed member */ );
597               icmp0->checksum = ip_csum_fold (sum0);
598             }
599         }
600       else
601         {
602           inner_ip0 = (ip4_header_t *) (echo0 + 1);
603           l4_header = ip4_next_header (inner_ip0);
604
605           if (!ip4_header_checksum_is_valid (inner_ip0))
606             {
607               next0 = SNAT_OUT2IN_NEXT_DROP;
608               goto out;
609             }
610
611           old_addr0 = inner_ip0->src_address.as_u32;
612           inner_ip0->src_address = addr;
613           new_addr0 = inner_ip0->src_address.as_u32;
614
615           sum0 = icmp0->checksum;
616           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
617                                  src_address /* changed member */ );
618           icmp0->checksum = ip_csum_fold (sum0);
619
620           switch (proto)
621             {
622             case NAT_PROTOCOL_ICMP:
623               inner_icmp0 = (icmp46_header_t *) l4_header;
624               inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
625
626               old_id0 = inner_echo0->identifier;
627               new_id0 = port;
628               inner_echo0->identifier = new_id0;
629
630               sum0 = icmp0->checksum;
631               sum0 =
632                 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
633                                 identifier);
634               icmp0->checksum = ip_csum_fold (sum0);
635               break;
636             case NAT_PROTOCOL_UDP:
637             case NAT_PROTOCOL_TCP:
638               old_id0 = ((tcp_udp_header_t *) l4_header)->src_port;
639               new_id0 = port;
640               ((tcp_udp_header_t *) l4_header)->src_port = new_id0;
641
642               sum0 = icmp0->checksum;
643               sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
644                                      src_port);
645               icmp0->checksum = ip_csum_fold (sum0);
646               break;
647             default:
648               ASSERT (0);
649             }
650         }
651     }
652
653 out:
654   return next0;
655 }
656 #endif
657
658 static inline u32
659 icmp_out2in_slow_path (snat_main_t * sm,
660                        vlib_buffer_t * b0,
661                        ip4_header_t * ip0,
662                        icmp46_header_t * icmp0,
663                        u32 sw_if_index0,
664                        u32 rx_fib_index0,
665                        vlib_node_runtime_t * node,
666                        u32 next0, f64 now,
667                        u32 thread_index, snat_session_t ** p_s0)
668 {
669   vlib_main_t *vm = vlib_get_main ();
670
671   next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
672                        next0, thread_index, p_s0);
673   snat_session_t *s0 = *p_s0;
674   if (PREDICT_TRUE (next0 != SNAT_OUT2IN_NEXT_DROP && s0))
675     {
676       /* Accounting */
677       nat44_ei_session_update_counters (
678         s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
679       /* Per-user LRU list maintenance */
680       nat44_session_update_lru (sm, s0, thread_index);
681     }
682   return next0;
683 }
684
685 static int
686 nat_out2in_sm_unknown_proto (snat_main_t * sm,
687                              vlib_buffer_t * b,
688                              ip4_header_t * ip, u32 rx_fib_index)
689 {
690   clib_bihash_kv_8_8_t kv, value;
691   snat_static_mapping_t *m;
692   u32 old_addr, new_addr;
693   ip_csum_t sum;
694
695   init_nat_k (&kv, ip->dst_address, 0, 0, 0);
696   if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
697     return 1;
698
699   m = pool_elt_at_index (sm->static_mappings, value.value);
700
701   old_addr = ip->dst_address.as_u32;
702   new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
703   sum = ip->checksum;
704   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
705   ip->checksum = ip_csum_fold (sum);
706
707   vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
708   return 0;
709 }
710
711 VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm,
712                                  vlib_node_runtime_t * node,
713                                  vlib_frame_t * frame)
714 {
715   u32 n_left_from, *from;
716   snat_main_t *sm = &snat_main;
717   f64 now = vlib_time_now (vm);
718   u32 thread_index = vm->thread_index;
719   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
720
721   from = vlib_frame_vector_args (frame);
722   n_left_from = frame->n_vectors;
723
724   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
725   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
726   vlib_get_buffers (vm, from, b, n_left_from);
727
728   while (n_left_from >= 2)
729     {
730       vlib_buffer_t *b0, *b1;
731       u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
732       u32 next1 = SNAT_OUT2IN_NEXT_LOOKUP;
733       u32 sw_if_index0, sw_if_index1;
734       ip4_header_t *ip0, *ip1;
735       ip_csum_t sum0, sum1;
736       u32 new_addr0, old_addr0;
737       u16 new_port0, old_port0;
738       u32 new_addr1, old_addr1;
739       u16 new_port1, old_port1;
740       udp_header_t *udp0, *udp1;
741       tcp_header_t *tcp0, *tcp1;
742       icmp46_header_t *icmp0, *icmp1;
743       u32 rx_fib_index0, rx_fib_index1;
744       u32 proto0, proto1;
745       snat_session_t *s0 = 0, *s1 = 0;
746       clib_bihash_kv_8_8_t kv0, kv1, value0, value1;
747       u8 identity_nat0, identity_nat1;
748       ip4_address_t sm_addr0, sm_addr1;
749       u16 sm_port0, sm_port1;
750       u32 sm_fib_index0, sm_fib_index1;
751
752       b0 = *b;
753       b++;
754       b1 = *b;
755       b++;
756
757       /* Prefetch next iteration. */
758       if (PREDICT_TRUE (n_left_from >= 4))
759         {
760           vlib_buffer_t *p2, *p3;
761
762           p2 = *b;
763           p3 = *(b + 1);
764
765           vlib_prefetch_buffer_header (p2, LOAD);
766           vlib_prefetch_buffer_header (p3, LOAD);
767
768           CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
769           CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
770         }
771
772       vnet_buffer (b0)->snat.flags = 0;
773       vnet_buffer (b1)->snat.flags = 0;
774
775       ip0 = vlib_buffer_get_current (b0);
776       udp0 = ip4_next_header (ip0);
777       tcp0 = (tcp_header_t *) udp0;
778       icmp0 = (icmp46_header_t *) udp0;
779
780       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
781       rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
782                                sw_if_index0);
783
784       if (PREDICT_FALSE (ip0->ttl == 1))
785         {
786           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
787           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
788                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
789                                        0);
790           next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
791           goto trace0;
792         }
793
794       proto0 = ip_proto_to_nat_proto (ip0->protocol);
795
796       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
797         {
798           if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
799             {
800               if (!sm->forwarding_enabled)
801                 {
802                   b0->error =
803                     node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
804                   next0 = SNAT_OUT2IN_NEXT_DROP;
805                 }
806             }
807           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
808                                          thread_index, sw_if_index0, 1);
809
810           goto trace0;
811         }
812
813       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
814         {
815           next0 = icmp_out2in_slow_path
816             (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
817              next0, now, thread_index, &s0);
818           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
819                                          thread_index, sw_if_index0, 1);
820           goto trace0;
821         }
822
823       init_nat_k (&kv0, ip0->dst_address,
824                   vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
825                   proto0);
826       if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
827         {
828           /* Try to match static mapping by external address and port,
829              destination address and port in packet */
830           if (nat44_ei_static_mapping_match (
831                 ip0->dst_address, vnet_buffer (b0)->ip.reass.l4_dst_port,
832                 rx_fib_index0, proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1,
833                 0, &identity_nat0))
834             {
835               /*
836                * Send DHCP packets to the ipv4 stack, or we won't
837                * be able to use dhcp client on the outside interface
838                */
839               if (PREDICT_FALSE
840                   (proto0 == NAT_PROTOCOL_UDP
841                    && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
842                        clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
843                 {
844                   vnet_feature_next (&next0, b0);
845                   goto trace0;
846                 }
847
848               if (!sm->forwarding_enabled)
849                 {
850                   b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
851                   next0 = SNAT_OUT2IN_NEXT_DROP;
852                 }
853               goto trace0;
854             }
855
856           if (PREDICT_FALSE (identity_nat0))
857             goto trace0;
858
859           /* Create session initiated by host from external network */
860           s0 = create_session_for_static_mapping (sm, b0,
861                                                   sm_addr0, sm_port0,
862                                                   sm_fib_index0,
863                                                   ip0->dst_address,
864                                                   vnet_buffer (b0)->ip.
865                                                   reass.l4_dst_port,
866                                                   rx_fib_index0, proto0, node,
867                                                   thread_index, now);
868           if (!s0)
869             {
870               next0 = SNAT_OUT2IN_NEXT_DROP;
871               goto trace0;
872             }
873         }
874       else
875         s0 = pool_elt_at_index (tsm->sessions,
876                                 nat_value_get_session_index (&value0));
877
878       old_addr0 = ip0->dst_address.as_u32;
879       ip0->dst_address = s0->in2out.addr;
880       new_addr0 = ip0->dst_address.as_u32;
881       vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
882
883       sum0 = ip0->checksum;
884       sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
885                              ip4_header_t, dst_address /* changed member */ );
886       ip0->checksum = ip_csum_fold (sum0);
887
888       if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
889         {
890           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
891             {
892               old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
893               new_port0 = udp0->dst_port = s0->in2out.port;
894               sum0 = tcp0->checksum;
895               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
896                                      ip4_header_t,
897                                      dst_address /* changed member */ );
898
899               sum0 = ip_csum_update (sum0, old_port0, new_port0,
900                                      ip4_header_t /* cheat */ ,
901                                      length /* changed member */ );
902               tcp0->checksum = ip_csum_fold (sum0);
903             }
904           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
905                                          thread_index, sw_if_index0, 1);
906         }
907       else
908         {
909           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
910             {
911               old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
912               new_port0 = udp0->dst_port = s0->in2out.port;
913               if (PREDICT_FALSE (udp0->checksum))
914                 {
915                   sum0 = udp0->checksum;
916                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address  /* changed member */
917                     );
918                   sum0 =
919                     ip_csum_update (sum0, old_port0, new_port0,
920                                     ip4_header_t /* cheat */ ,
921                                     length /* changed member */ );
922                   udp0->checksum = ip_csum_fold (sum0);
923                 }
924             }
925           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
926                                          thread_index, sw_if_index0, 1);
927         }
928
929       /* Accounting */
930       nat44_ei_session_update_counters (
931         s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
932       /* Per-user LRU list maintenance */
933       nat44_session_update_lru (sm, s0, thread_index);
934     trace0:
935
936       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
937                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
938         {
939           snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
940           t->sw_if_index = sw_if_index0;
941           t->next_index = next0;
942           t->session_index = ~0;
943           if (s0)
944             t->session_index =
945               s0 - sm->per_thread_data[thread_index].sessions;
946         }
947
948       if (next0 == SNAT_OUT2IN_NEXT_DROP)
949         {
950           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
951                                          thread_index, sw_if_index0, 1);
952         }
953
954
955       ip1 = vlib_buffer_get_current (b1);
956       udp1 = ip4_next_header (ip1);
957       tcp1 = (tcp_header_t *) udp1;
958       icmp1 = (icmp46_header_t *) udp1;
959
960       sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
961       rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
962                                sw_if_index1);
963
964       if (PREDICT_FALSE (ip1->ttl == 1))
965         {
966           vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
967           icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
968                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
969                                        0);
970           next1 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
971           goto trace1;
972         }
973
974       proto1 = ip_proto_to_nat_proto (ip1->protocol);
975
976       if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
977         {
978           if (nat_out2in_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
979             {
980               if (!sm->forwarding_enabled)
981                 {
982                   b1->error =
983                     node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
984                   next1 = SNAT_OUT2IN_NEXT_DROP;
985                 }
986             }
987           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
988                                          thread_index, sw_if_index1, 1);
989           goto trace1;
990         }
991
992       if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
993         {
994           next1 = icmp_out2in_slow_path
995             (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
996              next1, now, thread_index, &s1);
997           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
998                                          thread_index, sw_if_index1, 1);
999           goto trace1;
1000         }
1001
1002       init_nat_k (&kv1, ip1->dst_address,
1003                   vnet_buffer (b1)->ip.reass.l4_dst_port, rx_fib_index1,
1004                   proto1);
1005       if (clib_bihash_search_8_8 (&sm->out2in, &kv1, &value1))
1006         {
1007           /* Try to match static mapping by external address and port,
1008              destination address and port in packet */
1009           if (nat44_ei_static_mapping_match (
1010                 ip1->dst_address, vnet_buffer (b1)->ip.reass.l4_dst_port,
1011                 rx_fib_index1, proto1, &sm_addr1, &sm_port1, &sm_fib_index1, 1,
1012                 0, &identity_nat1))
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                   (proto1 == NAT_PROTOCOL_UDP
1020                    && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1021                        clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1022                 {
1023                   vnet_feature_next (&next1, b1);
1024                   goto trace1;
1025                 }
1026
1027               if (!sm->forwarding_enabled)
1028                 {
1029                   b1->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1030                   next1 = SNAT_OUT2IN_NEXT_DROP;
1031                 }
1032               goto trace1;
1033             }
1034
1035           if (PREDICT_FALSE (identity_nat1))
1036             goto trace1;
1037
1038           /* Create session initiated by host from external network */
1039           s1 =
1040             create_session_for_static_mapping (sm, b1, sm_addr1, sm_port1,
1041                                                sm_fib_index1,
1042                                                ip1->dst_address,
1043                                                vnet_buffer (b1)->ip.
1044                                                reass.l4_dst_port,
1045                                                rx_fib_index1, proto1, node,
1046                                                thread_index, now);
1047           if (!s1)
1048             {
1049               next1 = SNAT_OUT2IN_NEXT_DROP;
1050               goto trace1;
1051             }
1052         }
1053       else
1054         s1 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1055                                 nat_value_get_session_index (&value1));
1056
1057       old_addr1 = ip1->dst_address.as_u32;
1058       ip1->dst_address = s1->in2out.addr;
1059       new_addr1 = ip1->dst_address.as_u32;
1060       vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1061
1062       sum1 = ip1->checksum;
1063       sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1064                              ip4_header_t, dst_address /* changed member */ );
1065       ip1->checksum = ip_csum_fold (sum1);
1066
1067       if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1068         {
1069           if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1070             {
1071               old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1072               new_port1 = udp1->dst_port = s1->in2out.port;
1073
1074               sum1 = tcp1->checksum;
1075               sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1076                                      ip4_header_t,
1077                                      dst_address /* changed member */ );
1078
1079               sum1 = ip_csum_update (sum1, old_port1, new_port1,
1080                                      ip4_header_t /* cheat */ ,
1081                                      length /* changed member */ );
1082               tcp1->checksum = ip_csum_fold (sum1);
1083             }
1084           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1085                                          thread_index, sw_if_index1, 1);
1086         }
1087       else
1088         {
1089           if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1090             {
1091               old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1092               new_port1 = udp1->dst_port = s1->in2out.port;
1093               if (PREDICT_FALSE (udp1->checksum))
1094                 {
1095
1096                   sum1 = udp1->checksum;
1097                   sum1 =
1098                     ip_csum_update (sum1, old_addr1, new_addr1,
1099                                     ip4_header_t,
1100                                     dst_address /* changed member */ );
1101                   sum1 =
1102                     ip_csum_update (sum1, old_port1, new_port1,
1103                                     ip4_header_t /* cheat */ ,
1104                                     length /* changed member */ );
1105                   udp1->checksum = ip_csum_fold (sum1);
1106                 }
1107             }
1108           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1109                                          thread_index, sw_if_index1, 1);
1110         }
1111
1112       /* Accounting */
1113       nat44_ei_session_update_counters (
1114         s1, now, vlib_buffer_length_in_chain (vm, b1), thread_index);
1115       /* Per-user LRU list maintenance */
1116       nat44_session_update_lru (sm, s1, thread_index);
1117     trace1:
1118
1119       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1120                          && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1121         {
1122           snat_out2in_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
1123           t->sw_if_index = sw_if_index1;
1124           t->next_index = next1;
1125           t->session_index = ~0;
1126           if (s1)
1127             t->session_index =
1128               s1 - sm->per_thread_data[thread_index].sessions;
1129         }
1130
1131       if (next1 == SNAT_OUT2IN_NEXT_DROP)
1132         {
1133           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1134                                          thread_index, sw_if_index1, 1);
1135         }
1136
1137       n_left_from -= 2;
1138       next[0] = next0;
1139       next[1] = next1;
1140       next += 2;
1141     }
1142
1143   while (n_left_from > 0)
1144     {
1145       vlib_buffer_t *b0;
1146       u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1147       u32 sw_if_index0;
1148       ip4_header_t *ip0;
1149       ip_csum_t sum0;
1150       u32 new_addr0, old_addr0;
1151       u16 new_port0, old_port0;
1152       udp_header_t *udp0;
1153       tcp_header_t *tcp0;
1154       icmp46_header_t *icmp0;
1155       u32 rx_fib_index0;
1156       u32 proto0;
1157       snat_session_t *s0 = 0;
1158       clib_bihash_kv_8_8_t kv0, value0;
1159       u8 identity_nat0;
1160       ip4_address_t sm_addr0;
1161       u16 sm_port0;
1162       u32 sm_fib_index0;
1163
1164       b0 = *b;
1165       ++b;
1166
1167       vnet_buffer (b0)->snat.flags = 0;
1168
1169       ip0 = vlib_buffer_get_current (b0);
1170       udp0 = ip4_next_header (ip0);
1171       tcp0 = (tcp_header_t *) udp0;
1172       icmp0 = (icmp46_header_t *) udp0;
1173
1174       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1175       rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1176                                sw_if_index0);
1177
1178       proto0 = ip_proto_to_nat_proto (ip0->protocol);
1179
1180       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1181         {
1182           if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1183             {
1184               if (!sm->forwarding_enabled)
1185                 {
1186                   b0->error =
1187                     node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
1188                   next0 = SNAT_OUT2IN_NEXT_DROP;
1189                 }
1190             }
1191           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
1192                                          thread_index, sw_if_index0, 1);
1193           goto trace00;
1194         }
1195
1196       if (PREDICT_FALSE (ip0->ttl == 1))
1197         {
1198           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1199           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1200                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
1201                                        0);
1202           next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1203           goto trace00;
1204         }
1205
1206       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1207         {
1208           next0 = icmp_out2in_slow_path
1209             (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1210              next0, now, thread_index, &s0);
1211           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
1212                                          thread_index, sw_if_index0, 1);
1213           goto trace00;
1214         }
1215
1216       init_nat_k (&kv0, ip0->dst_address,
1217                   vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1218                   proto0);
1219
1220       if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
1221         {
1222           /* Try to match static mapping by external address and port,
1223              destination address and port in packet */
1224           if (nat44_ei_static_mapping_match (
1225                 ip0->dst_address, vnet_buffer (b0)->ip.reass.l4_dst_port,
1226                 rx_fib_index0, proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1,
1227                 0, &identity_nat0))
1228             {
1229               /*
1230                * Send DHCP packets to the ipv4 stack, or we won't
1231                * be able to use dhcp client on the outside interface
1232                */
1233               if (PREDICT_FALSE
1234                   (proto0 == NAT_PROTOCOL_UDP
1235                    && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1236                        clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1237                 {
1238                   vnet_feature_next (&next0, b0);
1239                   goto trace00;
1240                 }
1241
1242               if (!sm->forwarding_enabled)
1243                 {
1244                   b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1245                   next0 = SNAT_OUT2IN_NEXT_DROP;
1246                 }
1247               goto trace00;
1248             }
1249
1250           if (PREDICT_FALSE (identity_nat0))
1251             goto trace00;
1252
1253           /* Create session initiated by host from external network */
1254           s0 = create_session_for_static_mapping (sm, b0,
1255                                                   sm_addr0, sm_port0,
1256                                                   sm_fib_index0,
1257                                                   ip0->dst_address,
1258                                                   vnet_buffer (b0)->ip.
1259                                                   reass.l4_dst_port,
1260                                                   rx_fib_index0, proto0, node,
1261                                                   thread_index, now);
1262           if (!s0)
1263             {
1264               next0 = SNAT_OUT2IN_NEXT_DROP;
1265               goto trace00;
1266             }
1267         }
1268       else
1269         s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1270                                 nat_value_get_session_index (&value0));
1271
1272       old_addr0 = ip0->dst_address.as_u32;
1273       ip0->dst_address = s0->in2out.addr;
1274       new_addr0 = ip0->dst_address.as_u32;
1275       vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1276
1277       sum0 = ip0->checksum;
1278       sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1279                              ip4_header_t, dst_address /* changed member */ );
1280       ip0->checksum = ip_csum_fold (sum0);
1281
1282       if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1283         {
1284           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1285             {
1286               old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1287               new_port0 = udp0->dst_port = s0->in2out.port;
1288
1289               sum0 = tcp0->checksum;
1290               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1291                                      ip4_header_t,
1292                                      dst_address /* changed member */ );
1293
1294               sum0 = ip_csum_update (sum0, old_port0, new_port0,
1295                                      ip4_header_t /* cheat */ ,
1296                                      length /* changed member */ );
1297               tcp0->checksum = ip_csum_fold (sum0);
1298             }
1299           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1300                                          thread_index, sw_if_index0, 1);
1301         }
1302       else
1303         {
1304           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1305             {
1306               old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1307               new_port0 = udp0->dst_port = s0->in2out.port;
1308               if (PREDICT_FALSE (udp0->checksum))
1309                 {
1310                   sum0 = udp0->checksum;
1311                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address  /* changed member */
1312                     );
1313                   sum0 =
1314                     ip_csum_update (sum0, old_port0, new_port0,
1315                                     ip4_header_t /* cheat */ ,
1316                                     length /* changed member */ );
1317                   udp0->checksum = ip_csum_fold (sum0);
1318                 }
1319             }
1320           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1321                                          thread_index, sw_if_index0, 1);
1322         }
1323
1324       /* Accounting */
1325       nat44_ei_session_update_counters (
1326         s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
1327       /* Per-user LRU list maintenance */
1328       nat44_session_update_lru (sm, s0, thread_index);
1329     trace00:
1330
1331       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1332                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1333         {
1334           snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1335           t->sw_if_index = sw_if_index0;
1336           t->next_index = next0;
1337           t->session_index = ~0;
1338           if (s0)
1339             t->session_index =
1340               s0 - sm->per_thread_data[thread_index].sessions;
1341         }
1342
1343       if (next0 == SNAT_OUT2IN_NEXT_DROP)
1344         {
1345           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1346                                          thread_index, sw_if_index0, 1);
1347         }
1348
1349       n_left_from--;
1350       next[0] = next0;
1351       next++;
1352     }
1353
1354   vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1355                                frame->n_vectors);
1356
1357   return frame->n_vectors;
1358 }
1359
1360 /* *INDENT-OFF* */
1361 VLIB_REGISTER_NODE (snat_out2in_node) = {
1362   .name = "nat44-out2in",
1363   .vector_size = sizeof (u32),
1364   .format_trace = format_snat_out2in_trace,
1365   .type = VLIB_NODE_TYPE_INTERNAL,
1366
1367   .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1368   .error_strings = snat_out2in_error_strings,
1369
1370   .runtime_data_bytes = sizeof (snat_runtime_t),
1371
1372   .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1373
1374   /* edit / add dispositions here */
1375   .next_nodes = {
1376     [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1377     [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1378     [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1379   },
1380 };
1381 /* *INDENT-ON* */
1382
1383 VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm,
1384                                       vlib_node_runtime_t * node,
1385                                       vlib_frame_t * frame)
1386 {
1387   u32 n_left_from, *from;
1388   snat_main_t *sm = &snat_main;
1389
1390   from = vlib_frame_vector_args (frame);
1391   n_left_from = frame->n_vectors;
1392
1393   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1394   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1395   vlib_get_buffers (vm, from, b, n_left_from);
1396   while (n_left_from > 0)
1397     {
1398       vlib_buffer_t *b0;
1399       u32 next0 = SNAT_OUT2IN_NEXT_DROP;
1400       u32 sw_if_index0;
1401       ip4_header_t *ip0;
1402       ip_csum_t sum0;
1403       u32 new_addr0, old_addr0;
1404       u16 new_port0, old_port0;
1405       udp_header_t *udp0;
1406       tcp_header_t *tcp0;
1407       icmp46_header_t *icmp0;
1408       u32 proto0;
1409       u32 rx_fib_index0;
1410       ip4_address_t sm_addr0;
1411       u16 sm_port0;
1412       u32 sm_fib_index0;
1413
1414       b0 = *b;
1415       b++;
1416
1417       ip0 = vlib_buffer_get_current (b0);
1418       udp0 = ip4_next_header (ip0);
1419       tcp0 = (tcp_header_t *) udp0;
1420       icmp0 = (icmp46_header_t *) udp0;
1421
1422       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1423       rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1424
1425       vnet_feature_next (&next0, b0);
1426
1427       if (PREDICT_FALSE (ip0->ttl == 1))
1428         {
1429           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1430           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1431                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
1432                                        0);
1433           next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1434           goto trace00;
1435         }
1436
1437       proto0 = ip_proto_to_nat_proto (ip0->protocol);
1438
1439       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1440         goto trace00;
1441
1442       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1443         {
1444           next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
1445                                node, next0, ~0, 0);
1446           goto trace00;
1447         }
1448
1449       if (nat44_ei_static_mapping_match (ip0->dst_address, udp0->dst_port,
1450                                          rx_fib_index0, proto0, &sm_addr0,
1451                                          &sm_port0, &sm_fib_index0, 1, 0, 0))
1452         {
1453           b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1454           goto trace00;
1455         }
1456
1457       new_addr0 = sm_addr0.as_u32;
1458       new_port0 = sm_port0;
1459       vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm_fib_index0;
1460       old_addr0 = ip0->dst_address.as_u32;
1461       ip0->dst_address.as_u32 = new_addr0;
1462
1463       sum0 = ip0->checksum;
1464       sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1465                              ip4_header_t, dst_address /* changed member */ );
1466       ip0->checksum = ip_csum_fold (sum0);
1467
1468       if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1469         {
1470           old_port0 = udp0->dst_port;
1471           udp0->dst_port = new_port0;
1472
1473           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1474             {
1475               sum0 = tcp0->checksum;
1476               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1477                                      ip4_header_t,
1478                                      dst_address /* changed member */ );
1479               sum0 = ip_csum_update (sum0, old_port0, new_port0,
1480                                      ip4_header_t /* cheat */ ,
1481                                      length /* changed member */ );
1482               tcp0->checksum = ip_csum_fold (sum0);
1483             }
1484           else if (udp0->checksum)
1485             {
1486               sum0 = udp0->checksum;
1487               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1488                                      ip4_header_t,
1489                                      dst_address /* changed member */ );
1490               sum0 = ip_csum_update (sum0, old_port0, new_port0,
1491                                      ip4_header_t /* cheat */ ,
1492                                      length /* changed member */ );
1493               udp0->checksum = ip_csum_fold (sum0);
1494             }
1495         }
1496       else
1497         {
1498           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1499             {
1500               sum0 = tcp0->checksum;
1501               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1502                                      ip4_header_t,
1503                                      dst_address /* changed member */ );
1504               tcp0->checksum = ip_csum_fold (sum0);
1505             }
1506           else if (udp0->checksum)
1507             {
1508               sum0 = udp0->checksum;
1509               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1510                                      ip4_header_t,
1511                                      dst_address /* changed member */ );
1512               udp0->checksum = ip_csum_fold (sum0);
1513             }
1514         }
1515
1516     trace00:
1517
1518       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1519                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1520         {
1521           snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1522           t->sw_if_index = sw_if_index0;
1523           t->next_index = next0;
1524         }
1525
1526       if (next0 == SNAT_OUT2IN_NEXT_DROP)
1527         {
1528           vlib_increment_simple_counter (&sm->counters.fastpath.out2in.drops,
1529                                          vm->thread_index, sw_if_index0, 1);
1530         }
1531
1532       n_left_from--;
1533       next[0] = next0;
1534       next++;
1535     }
1536
1537   vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1538                                frame->n_vectors);
1539
1540   return frame->n_vectors;
1541 }
1542
1543 /* *INDENT-OFF* */
1544 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
1545   .name = "nat44-out2in-fast",
1546   .vector_size = sizeof (u32),
1547   .format_trace = format_snat_out2in_fast_trace,
1548   .type = VLIB_NODE_TYPE_INTERNAL,
1549
1550   .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1551   .error_strings = snat_out2in_error_strings,
1552
1553   .runtime_data_bytes = sizeof (snat_runtime_t),
1554
1555   .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1556
1557   /* edit / add dispositions here */
1558   .next_nodes = {
1559     [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1560     [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1561     [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1562   },
1563 };
1564 /* *INDENT-ON* */
1565
1566 /*
1567  * fd.io coding-style-patch-verification: ON
1568  *
1569  * Local Variables:
1570  * eval: (c-set-style "gnu")
1571  * End:
1572  */