nat: refactor and split fo EI/ED features p.2
[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 (&tsm->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, s - sm->per_thread_data[thread_index].sessions);
226   if (clib_bihash_add_or_overwrite_stale_8_8
227       (&sm->per_thread_data[thread_index].in2out, &kv0,
228        nat44_i2o_is_idle_session_cb, &ctx0))
229     nat_elog_notice ("in2out key add failed");
230
231   init_nat_o2i_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
232   if (clib_bihash_add_or_overwrite_stale_8_8
233       (&sm->per_thread_data[thread_index].out2in, &kv0,
234        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,
320                         ip4_header_t * ip0, ip4_address_t * addr,
321                         u16 * port, u32 * fib_index,
322                         nat_protocol_t * proto, void *d, void *e,
323                         u8 * dont_translate)
324 {
325   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
326   u32 sw_if_index0;
327   snat_session_t *s0 = 0;
328   clib_bihash_kv_8_8_t kv0, value0;
329   u8 is_addr_only;
330   u32 next0 = ~0;
331   int err;
332   u8 identity_nat;
333   vlib_main_t *vm = vlib_get_main ();
334   *dont_translate = 0;
335
336   sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
337   *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
338
339   *proto = 0;
340
341   err = icmp_get_key (b0, ip0, addr, port, proto);
342   if (err != -1)
343     {
344       b0->error = node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
345       next0 = SNAT_OUT2IN_NEXT_DROP;
346       goto out;
347     }
348
349   ip4_address_t mapping_addr;
350   u16 mapping_port;
351   u32 mapping_fib_index;
352
353   init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
354   if (clib_bihash_search_8_8 (&tsm->out2in, &kv0, &value0))
355     {
356       /* Try to match static mapping by external address and port,
357          destination address and port in packet */
358       if (nat44_ei_static_mapping_match (
359             *addr, *port, *fib_index, *proto, &mapping_addr, &mapping_port,
360             &mapping_fib_index, 1, &is_addr_only, &identity_nat))
361         {
362           if (!sm->forwarding_enabled)
363             {
364               /* Don't NAT packet aimed at the intfc address */
365               if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
366                                                     ip0->dst_address.as_u32)))
367                 {
368                   *dont_translate = 1;
369                   goto out;
370                 }
371               b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
372               next0 = SNAT_OUT2IN_NEXT_DROP;
373               goto out;
374             }
375           else
376             {
377               *dont_translate = 1;
378               goto out;
379             }
380         }
381
382       if (PREDICT_FALSE
383           (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
384            ICMP4_echo_reply
385            && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
386                ICMP4_echo_request || !is_addr_only)))
387         {
388           b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
389           next0 = SNAT_OUT2IN_NEXT_DROP;
390           goto out;
391         }
392
393       if (PREDICT_FALSE (identity_nat))
394         {
395           *dont_translate = 1;
396           goto out;
397         }
398       /* Create session initiated by host from external network */
399       s0 =
400         create_session_for_static_mapping (sm, b0, mapping_addr, mapping_port,
401                                            mapping_fib_index, *addr, *port,
402                                            *fib_index, *proto, node,
403                                            thread_index, vlib_time_now (vm));
404
405       if (!s0)
406         {
407           next0 = SNAT_OUT2IN_NEXT_DROP;
408           goto out;
409         }
410     }
411   else
412     {
413       if (PREDICT_FALSE
414           (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
415            ICMP4_echo_reply
416            && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
417            ICMP4_echo_request
418            && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
419                                            reass.icmp_type_or_tcp_flags)))
420         {
421           b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
422           next0 = SNAT_OUT2IN_NEXT_DROP;
423           goto out;
424         }
425
426       s0 = pool_elt_at_index (tsm->sessions, value0.value);
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 (d)
437     *(snat_session_t **) d = 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,
460                         ip4_header_t * ip0, ip4_address_t * mapping_addr,
461                         u16 * mapping_port, u32 * mapping_fib_index,
462                         nat_protocol_t * proto, void *d, void *e,
463                         u8 * dont_translate)
464 {
465   u32 sw_if_index0;
466   u32 rx_fib_index0;
467   u8 is_addr_only;
468   u32 next0 = ~0;
469   int err;
470   ip4_address_t addr;
471   u16 port;
472   *dont_translate = 0;
473
474   sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
475   rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
476
477   err = icmp_get_key (b0, ip0, &addr, &port, proto);
478   if (err != -1)
479     {
480       b0->error = node->errors[err];
481       next0 = SNAT_OUT2IN_NEXT_DROP;
482       goto out;
483     }
484   if (nat44_ei_static_mapping_match (addr, port, rx_fib_index0, *proto,
485                                      mapping_addr, mapping_port,
486                                      mapping_fib_index, 1, &is_addr_only, 0))
487     {
488       /* Don't NAT packet aimed at the intfc address */
489       if (is_interface_addr (sm, node, sw_if_index0, ip0->dst_address.as_u32))
490         {
491           *dont_translate = 1;
492           goto out;
493         }
494       b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
495       next0 = SNAT_OUT2IN_NEXT_DROP;
496       goto out;
497     }
498
499   if (PREDICT_FALSE
500       (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_reply
501        && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
502            ICMP4_echo_request || !is_addr_only)
503        && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
504                                        reass.icmp_type_or_tcp_flags)))
505     {
506       b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
507       next0 = SNAT_OUT2IN_NEXT_DROP;
508       goto out;
509     }
510
511 out:
512   return next0;
513 }
514 #endif
515
516 #ifndef CLIB_MARCH_VARIANT
517 u32
518 icmp_out2in (snat_main_t * sm,
519              vlib_buffer_t * b0,
520              ip4_header_t * ip0,
521              icmp46_header_t * icmp0,
522              u32 sw_if_index0,
523              u32 rx_fib_index0,
524              vlib_node_runtime_t * node,
525              u32 next0, u32 thread_index, void *d, void *e)
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 = sm->icmp_match_out2in_cb (sm, node, thread_index, b0, ip0,
546                                         &addr, &port, &fib_index, &proto,
547                                         d, e, &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, 0);
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
827           (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
828         {
829           /* Try to match static mapping by external address and port,
830              destination address and port in packet */
831           if (nat44_ei_static_mapping_match (
832                 ip0->dst_address, vnet_buffer (b0)->ip.reass.l4_dst_port,
833                 rx_fib_index0, proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1,
834                 0, &identity_nat0))
835             {
836               /*
837                * Send DHCP packets to the ipv4 stack, or we won't
838                * be able to use dhcp client on the outside interface
839                */
840               if (PREDICT_FALSE
841                   (proto0 == NAT_PROTOCOL_UDP
842                    && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
843                        clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
844                 {
845                   vnet_feature_next (&next0, b0);
846                   goto trace0;
847                 }
848
849               if (!sm->forwarding_enabled)
850                 {
851                   b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
852                   next0 = SNAT_OUT2IN_NEXT_DROP;
853                 }
854               goto trace0;
855             }
856
857           if (PREDICT_FALSE (identity_nat0))
858             goto trace0;
859
860           /* Create session initiated by host from external network */
861           s0 = create_session_for_static_mapping (sm, b0,
862                                                   sm_addr0, sm_port0,
863                                                   sm_fib_index0,
864                                                   ip0->dst_address,
865                                                   vnet_buffer (b0)->ip.
866                                                   reass.l4_dst_port,
867                                                   rx_fib_index0, proto0, node,
868                                                   thread_index, now);
869           if (!s0)
870             {
871               next0 = SNAT_OUT2IN_NEXT_DROP;
872               goto trace0;
873             }
874         }
875       else
876         s0 = pool_elt_at_index (tsm->sessions, value0.value);
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
1006           (&sm->per_thread_data[thread_index].out2in, &kv1, &value1))
1007         {
1008           /* Try to match static mapping by external address and port,
1009              destination address and port in packet */
1010           if (nat44_ei_static_mapping_match (
1011                 ip1->dst_address, vnet_buffer (b1)->ip.reass.l4_dst_port,
1012                 rx_fib_index1, proto1, &sm_addr1, &sm_port1, &sm_fib_index1, 1,
1013                 0, &identity_nat1))
1014             {
1015               /*
1016                * Send DHCP packets to the ipv4 stack, or we won't
1017                * be able to use dhcp client on the outside interface
1018                */
1019               if (PREDICT_FALSE
1020                   (proto1 == NAT_PROTOCOL_UDP
1021                    && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1022                        clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1023                 {
1024                   vnet_feature_next (&next1, b1);
1025                   goto trace1;
1026                 }
1027
1028               if (!sm->forwarding_enabled)
1029                 {
1030                   b1->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1031                   next1 = SNAT_OUT2IN_NEXT_DROP;
1032                 }
1033               goto trace1;
1034             }
1035
1036           if (PREDICT_FALSE (identity_nat1))
1037             goto trace1;
1038
1039           /* Create session initiated by host from external network */
1040           s1 =
1041             create_session_for_static_mapping (sm, b1, sm_addr1, sm_port1,
1042                                                sm_fib_index1,
1043                                                ip1->dst_address,
1044                                                vnet_buffer (b1)->ip.
1045                                                reass.l4_dst_port,
1046                                                rx_fib_index1, proto1, node,
1047                                                thread_index, now);
1048           if (!s1)
1049             {
1050               next1 = SNAT_OUT2IN_NEXT_DROP;
1051               goto trace1;
1052             }
1053         }
1054       else
1055         s1 =
1056           pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1057                              value1.value);
1058
1059       old_addr1 = ip1->dst_address.as_u32;
1060       ip1->dst_address = s1->in2out.addr;
1061       new_addr1 = ip1->dst_address.as_u32;
1062       vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1063
1064       sum1 = ip1->checksum;
1065       sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1066                              ip4_header_t, dst_address /* changed member */ );
1067       ip1->checksum = ip_csum_fold (sum1);
1068
1069       if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1070         {
1071           if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1072             {
1073               old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1074               new_port1 = udp1->dst_port = s1->in2out.port;
1075
1076               sum1 = tcp1->checksum;
1077               sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1078                                      ip4_header_t,
1079                                      dst_address /* changed member */ );
1080
1081               sum1 = ip_csum_update (sum1, old_port1, new_port1,
1082                                      ip4_header_t /* cheat */ ,
1083                                      length /* changed member */ );
1084               tcp1->checksum = ip_csum_fold (sum1);
1085             }
1086           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1087                                          thread_index, sw_if_index1, 1);
1088         }
1089       else
1090         {
1091           if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1092             {
1093               old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1094               new_port1 = udp1->dst_port = s1->in2out.port;
1095               if (PREDICT_FALSE (udp1->checksum))
1096                 {
1097
1098                   sum1 = udp1->checksum;
1099                   sum1 =
1100                     ip_csum_update (sum1, old_addr1, new_addr1,
1101                                     ip4_header_t,
1102                                     dst_address /* changed member */ );
1103                   sum1 =
1104                     ip_csum_update (sum1, old_port1, new_port1,
1105                                     ip4_header_t /* cheat */ ,
1106                                     length /* changed member */ );
1107                   udp1->checksum = ip_csum_fold (sum1);
1108                 }
1109             }
1110           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1111                                          thread_index, sw_if_index1, 1);
1112         }
1113
1114       /* Accounting */
1115       nat44_ei_session_update_counters (
1116         s1, now, vlib_buffer_length_in_chain (vm, b1), thread_index);
1117       /* Per-user LRU list maintenance */
1118       nat44_session_update_lru (sm, s1, thread_index);
1119     trace1:
1120
1121       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1122                          && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1123         {
1124           snat_out2in_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
1125           t->sw_if_index = sw_if_index1;
1126           t->next_index = next1;
1127           t->session_index = ~0;
1128           if (s1)
1129             t->session_index =
1130               s1 - sm->per_thread_data[thread_index].sessions;
1131         }
1132
1133       if (next1 == SNAT_OUT2IN_NEXT_DROP)
1134         {
1135           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1136                                          thread_index, sw_if_index1, 1);
1137         }
1138
1139       n_left_from -= 2;
1140       next[0] = next0;
1141       next[1] = next1;
1142       next += 2;
1143     }
1144
1145   while (n_left_from > 0)
1146     {
1147       vlib_buffer_t *b0;
1148       u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1149       u32 sw_if_index0;
1150       ip4_header_t *ip0;
1151       ip_csum_t sum0;
1152       u32 new_addr0, old_addr0;
1153       u16 new_port0, old_port0;
1154       udp_header_t *udp0;
1155       tcp_header_t *tcp0;
1156       icmp46_header_t *icmp0;
1157       u32 rx_fib_index0;
1158       u32 proto0;
1159       snat_session_t *s0 = 0;
1160       clib_bihash_kv_8_8_t kv0, value0;
1161       u8 identity_nat0;
1162       ip4_address_t sm_addr0;
1163       u16 sm_port0;
1164       u32 sm_fib_index0;
1165
1166       b0 = *b;
1167       ++b;
1168
1169       vnet_buffer (b0)->snat.flags = 0;
1170
1171       ip0 = vlib_buffer_get_current (b0);
1172       udp0 = ip4_next_header (ip0);
1173       tcp0 = (tcp_header_t *) udp0;
1174       icmp0 = (icmp46_header_t *) udp0;
1175
1176       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1177       rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1178                                sw_if_index0);
1179
1180       proto0 = ip_proto_to_nat_proto (ip0->protocol);
1181
1182       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1183         {
1184           if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1185             {
1186               if (!sm->forwarding_enabled)
1187                 {
1188                   b0->error =
1189                     node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
1190                   next0 = SNAT_OUT2IN_NEXT_DROP;
1191                 }
1192             }
1193           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
1194                                          thread_index, sw_if_index0, 1);
1195           goto trace00;
1196         }
1197
1198       if (PREDICT_FALSE (ip0->ttl == 1))
1199         {
1200           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1201           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1202                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
1203                                        0);
1204           next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1205           goto trace00;
1206         }
1207
1208       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1209         {
1210           next0 = icmp_out2in_slow_path
1211             (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1212              next0, now, thread_index, &s0);
1213           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
1214                                          thread_index, sw_if_index0, 1);
1215           goto trace00;
1216         }
1217
1218       init_nat_k (&kv0, ip0->dst_address,
1219                   vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1220                   proto0);
1221
1222       if (clib_bihash_search_8_8
1223           (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
1224         {
1225           /* Try to match static mapping by external address and port,
1226              destination address and port in packet */
1227           if (nat44_ei_static_mapping_match (
1228                 ip0->dst_address, vnet_buffer (b0)->ip.reass.l4_dst_port,
1229                 rx_fib_index0, proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1,
1230                 0, &identity_nat0))
1231             {
1232               /*
1233                * Send DHCP packets to the ipv4 stack, or we won't
1234                * be able to use dhcp client on the outside interface
1235                */
1236               if (PREDICT_FALSE
1237                   (proto0 == NAT_PROTOCOL_UDP
1238                    && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1239                        clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1240                 {
1241                   vnet_feature_next (&next0, b0);
1242                   goto trace00;
1243                 }
1244
1245               if (!sm->forwarding_enabled)
1246                 {
1247                   b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1248                   next0 = SNAT_OUT2IN_NEXT_DROP;
1249                 }
1250               goto trace00;
1251             }
1252
1253           if (PREDICT_FALSE (identity_nat0))
1254             goto trace00;
1255
1256           /* Create session initiated by host from external network */
1257           s0 = create_session_for_static_mapping (sm, b0,
1258                                                   sm_addr0, sm_port0,
1259                                                   sm_fib_index0,
1260                                                   ip0->dst_address,
1261                                                   vnet_buffer (b0)->ip.
1262                                                   reass.l4_dst_port,
1263                                                   rx_fib_index0, proto0, node,
1264                                                   thread_index, now);
1265           if (!s0)
1266             {
1267               next0 = SNAT_OUT2IN_NEXT_DROP;
1268               goto trace00;
1269             }
1270         }
1271       else
1272         s0 =
1273           pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1274                              value0.value);
1275
1276       old_addr0 = ip0->dst_address.as_u32;
1277       ip0->dst_address = s0->in2out.addr;
1278       new_addr0 = ip0->dst_address.as_u32;
1279       vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1280
1281       sum0 = ip0->checksum;
1282       sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1283                              ip4_header_t, dst_address /* changed member */ );
1284       ip0->checksum = ip_csum_fold (sum0);
1285
1286       if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1287         {
1288           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1289             {
1290               old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1291               new_port0 = udp0->dst_port = s0->in2out.port;
1292
1293               sum0 = tcp0->checksum;
1294               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1295                                      ip4_header_t,
1296                                      dst_address /* changed member */ );
1297
1298               sum0 = ip_csum_update (sum0, old_port0, new_port0,
1299                                      ip4_header_t /* cheat */ ,
1300                                      length /* changed member */ );
1301               tcp0->checksum = ip_csum_fold (sum0);
1302             }
1303           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1304                                          thread_index, sw_if_index0, 1);
1305         }
1306       else
1307         {
1308           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1309             {
1310               old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1311               new_port0 = udp0->dst_port = s0->in2out.port;
1312               if (PREDICT_FALSE (udp0->checksum))
1313                 {
1314                   sum0 = udp0->checksum;
1315                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address  /* changed member */
1316                     );
1317                   sum0 =
1318                     ip_csum_update (sum0, old_port0, new_port0,
1319                                     ip4_header_t /* cheat */ ,
1320                                     length /* changed member */ );
1321                   udp0->checksum = ip_csum_fold (sum0);
1322                 }
1323             }
1324           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1325                                          thread_index, sw_if_index0, 1);
1326         }
1327
1328       /* Accounting */
1329       nat44_ei_session_update_counters (
1330         s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
1331       /* Per-user LRU list maintenance */
1332       nat44_session_update_lru (sm, s0, thread_index);
1333     trace00:
1334
1335       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1336                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1337         {
1338           snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1339           t->sw_if_index = sw_if_index0;
1340           t->next_index = next0;
1341           t->session_index = ~0;
1342           if (s0)
1343             t->session_index =
1344               s0 - sm->per_thread_data[thread_index].sessions;
1345         }
1346
1347       if (next0 == SNAT_OUT2IN_NEXT_DROP)
1348         {
1349           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1350                                          thread_index, sw_if_index0, 1);
1351         }
1352
1353       n_left_from--;
1354       next[0] = next0;
1355       next++;
1356     }
1357
1358   vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1359                                frame->n_vectors);
1360
1361   return frame->n_vectors;
1362 }
1363
1364 /* *INDENT-OFF* */
1365 VLIB_REGISTER_NODE (snat_out2in_node) = {
1366   .name = "nat44-out2in",
1367   .vector_size = sizeof (u32),
1368   .format_trace = format_snat_out2in_trace,
1369   .type = VLIB_NODE_TYPE_INTERNAL,
1370
1371   .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1372   .error_strings = snat_out2in_error_strings,
1373
1374   .runtime_data_bytes = sizeof (snat_runtime_t),
1375
1376   .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1377
1378   /* edit / add dispositions here */
1379   .next_nodes = {
1380     [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1381     [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1382     [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1383   },
1384 };
1385 /* *INDENT-ON* */
1386
1387 VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm,
1388                                       vlib_node_runtime_t * node,
1389                                       vlib_frame_t * frame)
1390 {
1391   u32 n_left_from, *from;
1392   snat_main_t *sm = &snat_main;
1393
1394   from = vlib_frame_vector_args (frame);
1395   n_left_from = frame->n_vectors;
1396
1397   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1398   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1399   vlib_get_buffers (vm, from, b, n_left_from);
1400   while (n_left_from > 0)
1401     {
1402       vlib_buffer_t *b0;
1403       u32 next0 = SNAT_OUT2IN_NEXT_DROP;
1404       u32 sw_if_index0;
1405       ip4_header_t *ip0;
1406       ip_csum_t sum0;
1407       u32 new_addr0, old_addr0;
1408       u16 new_port0, old_port0;
1409       udp_header_t *udp0;
1410       tcp_header_t *tcp0;
1411       icmp46_header_t *icmp0;
1412       u32 proto0;
1413       u32 rx_fib_index0;
1414       ip4_address_t sm_addr0;
1415       u16 sm_port0;
1416       u32 sm_fib_index0;
1417
1418       b0 = *b;
1419       b++;
1420
1421       ip0 = vlib_buffer_get_current (b0);
1422       udp0 = ip4_next_header (ip0);
1423       tcp0 = (tcp_header_t *) udp0;
1424       icmp0 = (icmp46_header_t *) udp0;
1425
1426       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1427       rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1428
1429       vnet_feature_next (&next0, b0);
1430
1431       if (PREDICT_FALSE (ip0->ttl == 1))
1432         {
1433           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1434           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1435                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
1436                                        0);
1437           next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1438           goto trace00;
1439         }
1440
1441       proto0 = ip_proto_to_nat_proto (ip0->protocol);
1442
1443       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1444         goto trace00;
1445
1446       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1447         {
1448           next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
1449                                rx_fib_index0, node, next0, ~0, 0, 0);
1450           goto trace00;
1451         }
1452
1453       if (nat44_ei_static_mapping_match (ip0->dst_address, udp0->dst_port,
1454                                          rx_fib_index0, proto0, &sm_addr0,
1455                                          &sm_port0, &sm_fib_index0, 1, 0, 0))
1456         {
1457           b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1458           goto trace00;
1459         }
1460
1461       new_addr0 = sm_addr0.as_u32;
1462       new_port0 = sm_port0;
1463       vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm_fib_index0;
1464       old_addr0 = ip0->dst_address.as_u32;
1465       ip0->dst_address.as_u32 = new_addr0;
1466
1467       sum0 = ip0->checksum;
1468       sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1469                              ip4_header_t, dst_address /* changed member */ );
1470       ip0->checksum = ip_csum_fold (sum0);
1471
1472       if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1473         {
1474           old_port0 = udp0->dst_port;
1475           udp0->dst_port = new_port0;
1476
1477           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1478             {
1479               sum0 = tcp0->checksum;
1480               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1481                                      ip4_header_t,
1482                                      dst_address /* changed member */ );
1483               sum0 = ip_csum_update (sum0, old_port0, new_port0,
1484                                      ip4_header_t /* cheat */ ,
1485                                      length /* changed member */ );
1486               tcp0->checksum = ip_csum_fold (sum0);
1487             }
1488           else if (udp0->checksum)
1489             {
1490               sum0 = udp0->checksum;
1491               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1492                                      ip4_header_t,
1493                                      dst_address /* changed member */ );
1494               sum0 = ip_csum_update (sum0, old_port0, new_port0,
1495                                      ip4_header_t /* cheat */ ,
1496                                      length /* changed member */ );
1497               udp0->checksum = ip_csum_fold (sum0);
1498             }
1499         }
1500       else
1501         {
1502           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1503             {
1504               sum0 = tcp0->checksum;
1505               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1506                                      ip4_header_t,
1507                                      dst_address /* changed member */ );
1508               tcp0->checksum = ip_csum_fold (sum0);
1509             }
1510           else if (udp0->checksum)
1511             {
1512               sum0 = udp0->checksum;
1513               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1514                                      ip4_header_t,
1515                                      dst_address /* changed member */ );
1516               udp0->checksum = ip_csum_fold (sum0);
1517             }
1518         }
1519
1520     trace00:
1521
1522       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1523                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1524         {
1525           snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1526           t->sw_if_index = sw_if_index0;
1527           t->next_index = next0;
1528         }
1529
1530       if (next0 == SNAT_OUT2IN_NEXT_DROP)
1531         {
1532           vlib_increment_simple_counter (&sm->counters.fastpath.out2in.drops,
1533                                          vm->thread_index, sw_if_index0, 1);
1534         }
1535
1536       n_left_from--;
1537       next[0] = next0;
1538       next++;
1539     }
1540
1541   vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1542                                frame->n_vectors);
1543
1544   return frame->n_vectors;
1545 }
1546
1547 /* *INDENT-OFF* */
1548 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
1549   .name = "nat44-out2in-fast",
1550   .vector_size = sizeof (u32),
1551   .format_trace = format_snat_out2in_fast_trace,
1552   .type = VLIB_NODE_TYPE_INTERNAL,
1553
1554   .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1555   .error_strings = snat_out2in_error_strings,
1556
1557   .runtime_data_bytes = sizeof (snat_runtime_t),
1558
1559   .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1560
1561   /* edit / add dispositions here */
1562   .next_nodes = {
1563     [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1564     [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1565     [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1566   },
1567 };
1568 /* *INDENT-ON* */
1569
1570 /*
1571  * fd.io coding-style-patch-verification: ON
1572  *
1573  * Local Variables:
1574  * eval: (c-set-style "gnu")
1575  * End:
1576  */