nat: reduce number of hash tables for EI NAT
[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,
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 (&sm->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,
427                               nat_value_get_session_index (&value0));
428     }
429
430 out:
431   if (s0)
432     {
433       *addr = s0->in2out.addr;
434       *port = s0->in2out.port;
435       *fib_index = s0->in2out.fib_index;
436     }
437   if (d)
438     *(snat_session_t **) d = s0;
439   return next0;
440 }
441 #endif
442
443 #ifndef CLIB_MARCH_VARIANT
444 /**
445  * Get address and port values to be used for ICMP packet translation
446  *
447  * @param[in] sm                 NAT main
448  * @param[in,out] node           NAT node runtime
449  * @param[in] thread_index       thread index
450  * @param[in,out] b0             buffer containing packet to be translated
451  * @param[in,out] ip0            ip header
452  * @param[out] p_proto           protocol used for matching
453  * @param[out] p_value           address and port after NAT translation
454  * @param[out] p_dont_translate  if packet should not be translated
455  * @param d                      optional parameter
456  * @param e                      optional parameter
457  */
458 u32
459 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
460                         u32 thread_index, vlib_buffer_t * b0,
461                         ip4_header_t * ip0, ip4_address_t * mapping_addr,
462                         u16 * mapping_port, u32 * mapping_fib_index,
463                         nat_protocol_t * proto, void *d, void *e,
464                         u8 * dont_translate)
465 {
466   u32 sw_if_index0;
467   u32 rx_fib_index0;
468   u8 is_addr_only;
469   u32 next0 = ~0;
470   int err;
471   ip4_address_t addr;
472   u16 port;
473   *dont_translate = 0;
474
475   sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
476   rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
477
478   err = icmp_get_key (b0, ip0, &addr, &port, proto);
479   if (err != -1)
480     {
481       b0->error = node->errors[err];
482       next0 = SNAT_OUT2IN_NEXT_DROP;
483       goto out;
484     }
485   if (nat44_ei_static_mapping_match (addr, port, rx_fib_index0, *proto,
486                                      mapping_addr, mapping_port,
487                                      mapping_fib_index, 1, &is_addr_only, 0))
488     {
489       /* Don't NAT packet aimed at the intfc address */
490       if (is_interface_addr (sm, node, sw_if_index0, ip0->dst_address.as_u32))
491         {
492           *dont_translate = 1;
493           goto out;
494         }
495       b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
496       next0 = SNAT_OUT2IN_NEXT_DROP;
497       goto out;
498     }
499
500   if (PREDICT_FALSE
501       (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_reply
502        && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
503            ICMP4_echo_request || !is_addr_only)
504        && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
505                                        reass.icmp_type_or_tcp_flags)))
506     {
507       b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
508       next0 = SNAT_OUT2IN_NEXT_DROP;
509       goto out;
510     }
511
512 out:
513   return next0;
514 }
515 #endif
516
517 #ifndef CLIB_MARCH_VARIANT
518 u32
519 icmp_out2in (snat_main_t * sm,
520              vlib_buffer_t * b0,
521              ip4_header_t * ip0,
522              icmp46_header_t * icmp0,
523              u32 sw_if_index0,
524              u32 rx_fib_index0,
525              vlib_node_runtime_t * node,
526              u32 next0, u32 thread_index, void *d, void *e)
527 {
528   icmp_echo_header_t *echo0, *inner_echo0 = 0;
529   ip4_header_t *inner_ip0 = 0;
530   void *l4_header = 0;
531   icmp46_header_t *inner_icmp0;
532   u8 dont_translate;
533   u32 new_addr0, old_addr0;
534   u16 old_id0, new_id0;
535   ip_csum_t sum0;
536   u16 checksum0;
537   u32 next0_tmp;
538   vlib_main_t *vm = vlib_get_main ();
539   ip4_address_t addr;
540   u16 port;
541   u32 fib_index;
542   nat_protocol_t proto;
543
544   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
545
546   next0_tmp = sm->icmp_match_out2in_cb (sm, node, thread_index, b0, ip0,
547                                         &addr, &port, &fib_index, &proto,
548                                         d, e, &dont_translate);
549   if (next0_tmp != ~0)
550     next0 = next0_tmp;
551   if (next0 == SNAT_OUT2IN_NEXT_DROP || dont_translate)
552     goto out;
553
554   if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
555     {
556       sum0 =
557         ip_incremental_checksum_buffer (vm, b0,
558                                         (u8 *) icmp0 -
559                                         (u8 *) vlib_buffer_get_current (b0),
560                                         ntohs (ip0->length) -
561                                         ip4_header_bytes (ip0), 0);
562       checksum0 = ~ip_csum_fold (sum0);
563       if (checksum0 != 0 && checksum0 != 0xffff)
564         {
565           next0 = SNAT_OUT2IN_NEXT_DROP;
566           goto out;
567         }
568     }
569
570   old_addr0 = ip0->dst_address.as_u32;
571   new_addr0 = ip0->dst_address.as_u32 = addr.as_u32;
572   vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
573
574   sum0 = ip0->checksum;
575   sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
576                          dst_address /* changed member */ );
577   ip0->checksum = ip_csum_fold (sum0);
578
579
580   if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
581     {
582       if (icmp0->checksum == 0)
583         icmp0->checksum = 0xffff;
584
585       if (!icmp_type_is_error_message (icmp0->type))
586         {
587           new_id0 = port;
588           if (PREDICT_FALSE (new_id0 != echo0->identifier))
589             {
590               old_id0 = echo0->identifier;
591               new_id0 = port;
592               echo0->identifier = new_id0;
593
594               sum0 = icmp0->checksum;
595               sum0 =
596                 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
597                                 identifier /* changed member */ );
598               icmp0->checksum = ip_csum_fold (sum0);
599             }
600         }
601       else
602         {
603           inner_ip0 = (ip4_header_t *) (echo0 + 1);
604           l4_header = ip4_next_header (inner_ip0);
605
606           if (!ip4_header_checksum_is_valid (inner_ip0))
607             {
608               next0 = SNAT_OUT2IN_NEXT_DROP;
609               goto out;
610             }
611
612           old_addr0 = inner_ip0->src_address.as_u32;
613           inner_ip0->src_address = addr;
614           new_addr0 = inner_ip0->src_address.as_u32;
615
616           sum0 = icmp0->checksum;
617           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
618                                  src_address /* changed member */ );
619           icmp0->checksum = ip_csum_fold (sum0);
620
621           switch (proto)
622             {
623             case NAT_PROTOCOL_ICMP:
624               inner_icmp0 = (icmp46_header_t *) l4_header;
625               inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
626
627               old_id0 = inner_echo0->identifier;
628               new_id0 = port;
629               inner_echo0->identifier = new_id0;
630
631               sum0 = icmp0->checksum;
632               sum0 =
633                 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
634                                 identifier);
635               icmp0->checksum = ip_csum_fold (sum0);
636               break;
637             case NAT_PROTOCOL_UDP:
638             case NAT_PROTOCOL_TCP:
639               old_id0 = ((tcp_udp_header_t *) l4_header)->src_port;
640               new_id0 = port;
641               ((tcp_udp_header_t *) l4_header)->src_port = new_id0;
642
643               sum0 = icmp0->checksum;
644               sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
645                                      src_port);
646               icmp0->checksum = ip_csum_fold (sum0);
647               break;
648             default:
649               ASSERT (0);
650             }
651         }
652     }
653
654 out:
655   return next0;
656 }
657 #endif
658
659 static inline u32
660 icmp_out2in_slow_path (snat_main_t * sm,
661                        vlib_buffer_t * b0,
662                        ip4_header_t * ip0,
663                        icmp46_header_t * icmp0,
664                        u32 sw_if_index0,
665                        u32 rx_fib_index0,
666                        vlib_node_runtime_t * node,
667                        u32 next0, f64 now,
668                        u32 thread_index, snat_session_t ** p_s0)
669 {
670   vlib_main_t *vm = vlib_get_main ();
671
672   next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
673                        next0, thread_index, p_s0, 0);
674   snat_session_t *s0 = *p_s0;
675   if (PREDICT_TRUE (next0 != SNAT_OUT2IN_NEXT_DROP && s0))
676     {
677       /* Accounting */
678       nat44_ei_session_update_counters (
679         s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
680       /* Per-user LRU list maintenance */
681       nat44_session_update_lru (sm, s0, thread_index);
682     }
683   return next0;
684 }
685
686 static int
687 nat_out2in_sm_unknown_proto (snat_main_t * sm,
688                              vlib_buffer_t * b,
689                              ip4_header_t * ip, u32 rx_fib_index)
690 {
691   clib_bihash_kv_8_8_t kv, value;
692   snat_static_mapping_t *m;
693   u32 old_addr, new_addr;
694   ip_csum_t sum;
695
696   init_nat_k (&kv, ip->dst_address, 0, 0, 0);
697   if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
698     return 1;
699
700   m = pool_elt_at_index (sm->static_mappings, value.value);
701
702   old_addr = ip->dst_address.as_u32;
703   new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
704   sum = ip->checksum;
705   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
706   ip->checksum = ip_csum_fold (sum);
707
708   vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
709   return 0;
710 }
711
712 VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm,
713                                  vlib_node_runtime_t * node,
714                                  vlib_frame_t * frame)
715 {
716   u32 n_left_from, *from;
717   snat_main_t *sm = &snat_main;
718   f64 now = vlib_time_now (vm);
719   u32 thread_index = vm->thread_index;
720   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
721
722   from = vlib_frame_vector_args (frame);
723   n_left_from = frame->n_vectors;
724
725   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
726   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
727   vlib_get_buffers (vm, from, b, n_left_from);
728
729   while (n_left_from >= 2)
730     {
731       vlib_buffer_t *b0, *b1;
732       u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
733       u32 next1 = SNAT_OUT2IN_NEXT_LOOKUP;
734       u32 sw_if_index0, sw_if_index1;
735       ip4_header_t *ip0, *ip1;
736       ip_csum_t sum0, sum1;
737       u32 new_addr0, old_addr0;
738       u16 new_port0, old_port0;
739       u32 new_addr1, old_addr1;
740       u16 new_port1, old_port1;
741       udp_header_t *udp0, *udp1;
742       tcp_header_t *tcp0, *tcp1;
743       icmp46_header_t *icmp0, *icmp1;
744       u32 rx_fib_index0, rx_fib_index1;
745       u32 proto0, proto1;
746       snat_session_t *s0 = 0, *s1 = 0;
747       clib_bihash_kv_8_8_t kv0, kv1, value0, value1;
748       u8 identity_nat0, identity_nat1;
749       ip4_address_t sm_addr0, sm_addr1;
750       u16 sm_port0, sm_port1;
751       u32 sm_fib_index0, sm_fib_index1;
752
753       b0 = *b;
754       b++;
755       b1 = *b;
756       b++;
757
758       /* Prefetch next iteration. */
759       if (PREDICT_TRUE (n_left_from >= 4))
760         {
761           vlib_buffer_t *p2, *p3;
762
763           p2 = *b;
764           p3 = *(b + 1);
765
766           vlib_prefetch_buffer_header (p2, LOAD);
767           vlib_prefetch_buffer_header (p3, LOAD);
768
769           CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
770           CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
771         }
772
773       vnet_buffer (b0)->snat.flags = 0;
774       vnet_buffer (b1)->snat.flags = 0;
775
776       ip0 = vlib_buffer_get_current (b0);
777       udp0 = ip4_next_header (ip0);
778       tcp0 = (tcp_header_t *) udp0;
779       icmp0 = (icmp46_header_t *) udp0;
780
781       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
782       rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
783                                sw_if_index0);
784
785       if (PREDICT_FALSE (ip0->ttl == 1))
786         {
787           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
788           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
789                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
790                                        0);
791           next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
792           goto trace0;
793         }
794
795       proto0 = ip_proto_to_nat_proto (ip0->protocol);
796
797       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
798         {
799           if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
800             {
801               if (!sm->forwarding_enabled)
802                 {
803                   b0->error =
804                     node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
805                   next0 = SNAT_OUT2IN_NEXT_DROP;
806                 }
807             }
808           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
809                                          thread_index, sw_if_index0, 1);
810
811           goto trace0;
812         }
813
814       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
815         {
816           next0 = icmp_out2in_slow_path
817             (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
818              next0, now, thread_index, &s0);
819           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
820                                          thread_index, sw_if_index0, 1);
821           goto trace0;
822         }
823
824       init_nat_k (&kv0, ip0->dst_address,
825                   vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
826                   proto0);
827       if (clib_bihash_search_8_8 (&sm->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,
877                                 nat_value_get_session_index (&value0));
878
879       old_addr0 = ip0->dst_address.as_u32;
880       ip0->dst_address = s0->in2out.addr;
881       new_addr0 = ip0->dst_address.as_u32;
882       vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
883
884       sum0 = ip0->checksum;
885       sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
886                              ip4_header_t, dst_address /* changed member */ );
887       ip0->checksum = ip_csum_fold (sum0);
888
889       if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
890         {
891           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
892             {
893               old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
894               new_port0 = udp0->dst_port = s0->in2out.port;
895               sum0 = tcp0->checksum;
896               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
897                                      ip4_header_t,
898                                      dst_address /* changed member */ );
899
900               sum0 = ip_csum_update (sum0, old_port0, new_port0,
901                                      ip4_header_t /* cheat */ ,
902                                      length /* changed member */ );
903               tcp0->checksum = ip_csum_fold (sum0);
904             }
905           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
906                                          thread_index, sw_if_index0, 1);
907         }
908       else
909         {
910           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
911             {
912               old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
913               new_port0 = udp0->dst_port = s0->in2out.port;
914               if (PREDICT_FALSE (udp0->checksum))
915                 {
916                   sum0 = udp0->checksum;
917                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address  /* changed member */
918                     );
919                   sum0 =
920                     ip_csum_update (sum0, old_port0, new_port0,
921                                     ip4_header_t /* cheat */ ,
922                                     length /* changed member */ );
923                   udp0->checksum = ip_csum_fold (sum0);
924                 }
925             }
926           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
927                                          thread_index, sw_if_index0, 1);
928         }
929
930       /* Accounting */
931       nat44_ei_session_update_counters (
932         s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
933       /* Per-user LRU list maintenance */
934       nat44_session_update_lru (sm, s0, thread_index);
935     trace0:
936
937       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
938                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
939         {
940           snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
941           t->sw_if_index = sw_if_index0;
942           t->next_index = next0;
943           t->session_index = ~0;
944           if (s0)
945             t->session_index =
946               s0 - sm->per_thread_data[thread_index].sessions;
947         }
948
949       if (next0 == SNAT_OUT2IN_NEXT_DROP)
950         {
951           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
952                                          thread_index, sw_if_index0, 1);
953         }
954
955
956       ip1 = vlib_buffer_get_current (b1);
957       udp1 = ip4_next_header (ip1);
958       tcp1 = (tcp_header_t *) udp1;
959       icmp1 = (icmp46_header_t *) udp1;
960
961       sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
962       rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
963                                sw_if_index1);
964
965       if (PREDICT_FALSE (ip1->ttl == 1))
966         {
967           vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
968           icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
969                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
970                                        0);
971           next1 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
972           goto trace1;
973         }
974
975       proto1 = ip_proto_to_nat_proto (ip1->protocol);
976
977       if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
978         {
979           if (nat_out2in_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
980             {
981               if (!sm->forwarding_enabled)
982                 {
983                   b1->error =
984                     node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
985                   next1 = SNAT_OUT2IN_NEXT_DROP;
986                 }
987             }
988           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
989                                          thread_index, sw_if_index1, 1);
990           goto trace1;
991         }
992
993       if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
994         {
995           next1 = icmp_out2in_slow_path
996             (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
997              next1, now, thread_index, &s1);
998           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
999                                          thread_index, sw_if_index1, 1);
1000           goto trace1;
1001         }
1002
1003       init_nat_k (&kv1, ip1->dst_address,
1004                   vnet_buffer (b1)->ip.reass.l4_dst_port, rx_fib_index1,
1005                   proto1);
1006       if (clib_bihash_search_8_8 (&sm->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 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1056                                 nat_value_get_session_index (&value1));
1057
1058       old_addr1 = ip1->dst_address.as_u32;
1059       ip1->dst_address = s1->in2out.addr;
1060       new_addr1 = ip1->dst_address.as_u32;
1061       vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1062
1063       sum1 = ip1->checksum;
1064       sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1065                              ip4_header_t, dst_address /* changed member */ );
1066       ip1->checksum = ip_csum_fold (sum1);
1067
1068       if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1069         {
1070           if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1071             {
1072               old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1073               new_port1 = udp1->dst_port = s1->in2out.port;
1074
1075               sum1 = tcp1->checksum;
1076               sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1077                                      ip4_header_t,
1078                                      dst_address /* changed member */ );
1079
1080               sum1 = ip_csum_update (sum1, old_port1, new_port1,
1081                                      ip4_header_t /* cheat */ ,
1082                                      length /* changed member */ );
1083               tcp1->checksum = ip_csum_fold (sum1);
1084             }
1085           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1086                                          thread_index, sw_if_index1, 1);
1087         }
1088       else
1089         {
1090           if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1091             {
1092               old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1093               new_port1 = udp1->dst_port = s1->in2out.port;
1094               if (PREDICT_FALSE (udp1->checksum))
1095                 {
1096
1097                   sum1 = udp1->checksum;
1098                   sum1 =
1099                     ip_csum_update (sum1, old_addr1, new_addr1,
1100                                     ip4_header_t,
1101                                     dst_address /* changed member */ );
1102                   sum1 =
1103                     ip_csum_update (sum1, old_port1, new_port1,
1104                                     ip4_header_t /* cheat */ ,
1105                                     length /* changed member */ );
1106                   udp1->checksum = ip_csum_fold (sum1);
1107                 }
1108             }
1109           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1110                                          thread_index, sw_if_index1, 1);
1111         }
1112
1113       /* Accounting */
1114       nat44_ei_session_update_counters (
1115         s1, now, vlib_buffer_length_in_chain (vm, b1), thread_index);
1116       /* Per-user LRU list maintenance */
1117       nat44_session_update_lru (sm, s1, thread_index);
1118     trace1:
1119
1120       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1121                          && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1122         {
1123           snat_out2in_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
1124           t->sw_if_index = sw_if_index1;
1125           t->next_index = next1;
1126           t->session_index = ~0;
1127           if (s1)
1128             t->session_index =
1129               s1 - sm->per_thread_data[thread_index].sessions;
1130         }
1131
1132       if (next1 == SNAT_OUT2IN_NEXT_DROP)
1133         {
1134           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1135                                          thread_index, sw_if_index1, 1);
1136         }
1137
1138       n_left_from -= 2;
1139       next[0] = next0;
1140       next[1] = next1;
1141       next += 2;
1142     }
1143
1144   while (n_left_from > 0)
1145     {
1146       vlib_buffer_t *b0;
1147       u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1148       u32 sw_if_index0;
1149       ip4_header_t *ip0;
1150       ip_csum_t sum0;
1151       u32 new_addr0, old_addr0;
1152       u16 new_port0, old_port0;
1153       udp_header_t *udp0;
1154       tcp_header_t *tcp0;
1155       icmp46_header_t *icmp0;
1156       u32 rx_fib_index0;
1157       u32 proto0;
1158       snat_session_t *s0 = 0;
1159       clib_bihash_kv_8_8_t kv0, value0;
1160       u8 identity_nat0;
1161       ip4_address_t sm_addr0;
1162       u16 sm_port0;
1163       u32 sm_fib_index0;
1164
1165       b0 = *b;
1166       ++b;
1167
1168       vnet_buffer (b0)->snat.flags = 0;
1169
1170       ip0 = vlib_buffer_get_current (b0);
1171       udp0 = ip4_next_header (ip0);
1172       tcp0 = (tcp_header_t *) udp0;
1173       icmp0 = (icmp46_header_t *) udp0;
1174
1175       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1176       rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1177                                sw_if_index0);
1178
1179       proto0 = ip_proto_to_nat_proto (ip0->protocol);
1180
1181       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1182         {
1183           if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1184             {
1185               if (!sm->forwarding_enabled)
1186                 {
1187                   b0->error =
1188                     node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
1189                   next0 = SNAT_OUT2IN_NEXT_DROP;
1190                 }
1191             }
1192           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
1193                                          thread_index, sw_if_index0, 1);
1194           goto trace00;
1195         }
1196
1197       if (PREDICT_FALSE (ip0->ttl == 1))
1198         {
1199           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1200           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1201                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
1202                                        0);
1203           next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1204           goto trace00;
1205         }
1206
1207       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1208         {
1209           next0 = icmp_out2in_slow_path
1210             (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1211              next0, now, thread_index, &s0);
1212           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
1213                                          thread_index, sw_if_index0, 1);
1214           goto trace00;
1215         }
1216
1217       init_nat_k (&kv0, ip0->dst_address,
1218                   vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1219                   proto0);
1220
1221       if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
1222         {
1223           /* Try to match static mapping by external address and port,
1224              destination address and port in packet */
1225           if (nat44_ei_static_mapping_match (
1226                 ip0->dst_address, vnet_buffer (b0)->ip.reass.l4_dst_port,
1227                 rx_fib_index0, proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1,
1228                 0, &identity_nat0))
1229             {
1230               /*
1231                * Send DHCP packets to the ipv4 stack, or we won't
1232                * be able to use dhcp client on the outside interface
1233                */
1234               if (PREDICT_FALSE
1235                   (proto0 == NAT_PROTOCOL_UDP
1236                    && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1237                        clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1238                 {
1239                   vnet_feature_next (&next0, b0);
1240                   goto trace00;
1241                 }
1242
1243               if (!sm->forwarding_enabled)
1244                 {
1245                   b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1246                   next0 = SNAT_OUT2IN_NEXT_DROP;
1247                 }
1248               goto trace00;
1249             }
1250
1251           if (PREDICT_FALSE (identity_nat0))
1252             goto trace00;
1253
1254           /* Create session initiated by host from external network */
1255           s0 = create_session_for_static_mapping (sm, b0,
1256                                                   sm_addr0, sm_port0,
1257                                                   sm_fib_index0,
1258                                                   ip0->dst_address,
1259                                                   vnet_buffer (b0)->ip.
1260                                                   reass.l4_dst_port,
1261                                                   rx_fib_index0, proto0, node,
1262                                                   thread_index, now);
1263           if (!s0)
1264             {
1265               next0 = SNAT_OUT2IN_NEXT_DROP;
1266               goto trace00;
1267             }
1268         }
1269       else
1270         s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1271                                 nat_value_get_session_index (&value0));
1272
1273       old_addr0 = ip0->dst_address.as_u32;
1274       ip0->dst_address = s0->in2out.addr;
1275       new_addr0 = ip0->dst_address.as_u32;
1276       vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1277
1278       sum0 = ip0->checksum;
1279       sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1280                              ip4_header_t, dst_address /* changed member */ );
1281       ip0->checksum = ip_csum_fold (sum0);
1282
1283       if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1284         {
1285           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1286             {
1287               old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1288               new_port0 = udp0->dst_port = s0->in2out.port;
1289
1290               sum0 = tcp0->checksum;
1291               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1292                                      ip4_header_t,
1293                                      dst_address /* changed member */ );
1294
1295               sum0 = ip_csum_update (sum0, old_port0, new_port0,
1296                                      ip4_header_t /* cheat */ ,
1297                                      length /* changed member */ );
1298               tcp0->checksum = ip_csum_fold (sum0);
1299             }
1300           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1301                                          thread_index, sw_if_index0, 1);
1302         }
1303       else
1304         {
1305           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1306             {
1307               old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1308               new_port0 = udp0->dst_port = s0->in2out.port;
1309               if (PREDICT_FALSE (udp0->checksum))
1310                 {
1311                   sum0 = udp0->checksum;
1312                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address  /* changed member */
1313                     );
1314                   sum0 =
1315                     ip_csum_update (sum0, old_port0, new_port0,
1316                                     ip4_header_t /* cheat */ ,
1317                                     length /* changed member */ );
1318                   udp0->checksum = ip_csum_fold (sum0);
1319                 }
1320             }
1321           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1322                                          thread_index, sw_if_index0, 1);
1323         }
1324
1325       /* Accounting */
1326       nat44_ei_session_update_counters (
1327         s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
1328       /* Per-user LRU list maintenance */
1329       nat44_session_update_lru (sm, s0, thread_index);
1330     trace00:
1331
1332       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1333                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1334         {
1335           snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1336           t->sw_if_index = sw_if_index0;
1337           t->next_index = next0;
1338           t->session_index = ~0;
1339           if (s0)
1340             t->session_index =
1341               s0 - sm->per_thread_data[thread_index].sessions;
1342         }
1343
1344       if (next0 == SNAT_OUT2IN_NEXT_DROP)
1345         {
1346           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1347                                          thread_index, sw_if_index0, 1);
1348         }
1349
1350       n_left_from--;
1351       next[0] = next0;
1352       next++;
1353     }
1354
1355   vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1356                                frame->n_vectors);
1357
1358   return frame->n_vectors;
1359 }
1360
1361 /* *INDENT-OFF* */
1362 VLIB_REGISTER_NODE (snat_out2in_node) = {
1363   .name = "nat44-out2in",
1364   .vector_size = sizeof (u32),
1365   .format_trace = format_snat_out2in_trace,
1366   .type = VLIB_NODE_TYPE_INTERNAL,
1367
1368   .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1369   .error_strings = snat_out2in_error_strings,
1370
1371   .runtime_data_bytes = sizeof (snat_runtime_t),
1372
1373   .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1374
1375   /* edit / add dispositions here */
1376   .next_nodes = {
1377     [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1378     [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1379     [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1380   },
1381 };
1382 /* *INDENT-ON* */
1383
1384 VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm,
1385                                       vlib_node_runtime_t * node,
1386                                       vlib_frame_t * frame)
1387 {
1388   u32 n_left_from, *from;
1389   snat_main_t *sm = &snat_main;
1390
1391   from = vlib_frame_vector_args (frame);
1392   n_left_from = frame->n_vectors;
1393
1394   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1395   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1396   vlib_get_buffers (vm, from, b, n_left_from);
1397   while (n_left_from > 0)
1398     {
1399       vlib_buffer_t *b0;
1400       u32 next0 = SNAT_OUT2IN_NEXT_DROP;
1401       u32 sw_if_index0;
1402       ip4_header_t *ip0;
1403       ip_csum_t sum0;
1404       u32 new_addr0, old_addr0;
1405       u16 new_port0, old_port0;
1406       udp_header_t *udp0;
1407       tcp_header_t *tcp0;
1408       icmp46_header_t *icmp0;
1409       u32 proto0;
1410       u32 rx_fib_index0;
1411       ip4_address_t sm_addr0;
1412       u16 sm_port0;
1413       u32 sm_fib_index0;
1414
1415       b0 = *b;
1416       b++;
1417
1418       ip0 = vlib_buffer_get_current (b0);
1419       udp0 = ip4_next_header (ip0);
1420       tcp0 = (tcp_header_t *) udp0;
1421       icmp0 = (icmp46_header_t *) udp0;
1422
1423       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1424       rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1425
1426       vnet_feature_next (&next0, b0);
1427
1428       if (PREDICT_FALSE (ip0->ttl == 1))
1429         {
1430           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1431           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1432                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
1433                                        0);
1434           next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1435           goto trace00;
1436         }
1437
1438       proto0 = ip_proto_to_nat_proto (ip0->protocol);
1439
1440       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1441         goto trace00;
1442
1443       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1444         {
1445           next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
1446                                rx_fib_index0, node, next0, ~0, 0, 0);
1447           goto trace00;
1448         }
1449
1450       if (nat44_ei_static_mapping_match (ip0->dst_address, udp0->dst_port,
1451                                          rx_fib_index0, proto0, &sm_addr0,
1452                                          &sm_port0, &sm_fib_index0, 1, 0, 0))
1453         {
1454           b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1455           goto trace00;
1456         }
1457
1458       new_addr0 = sm_addr0.as_u32;
1459       new_port0 = sm_port0;
1460       vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm_fib_index0;
1461       old_addr0 = ip0->dst_address.as_u32;
1462       ip0->dst_address.as_u32 = new_addr0;
1463
1464       sum0 = ip0->checksum;
1465       sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1466                              ip4_header_t, dst_address /* changed member */ );
1467       ip0->checksum = ip_csum_fold (sum0);
1468
1469       if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1470         {
1471           old_port0 = udp0->dst_port;
1472           udp0->dst_port = new_port0;
1473
1474           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1475             {
1476               sum0 = tcp0->checksum;
1477               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1478                                      ip4_header_t,
1479                                      dst_address /* changed member */ );
1480               sum0 = ip_csum_update (sum0, old_port0, new_port0,
1481                                      ip4_header_t /* cheat */ ,
1482                                      length /* changed member */ );
1483               tcp0->checksum = ip_csum_fold (sum0);
1484             }
1485           else if (udp0->checksum)
1486             {
1487               sum0 = udp0->checksum;
1488               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1489                                      ip4_header_t,
1490                                      dst_address /* changed member */ );
1491               sum0 = ip_csum_update (sum0, old_port0, new_port0,
1492                                      ip4_header_t /* cheat */ ,
1493                                      length /* changed member */ );
1494               udp0->checksum = ip_csum_fold (sum0);
1495             }
1496         }
1497       else
1498         {
1499           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1500             {
1501               sum0 = tcp0->checksum;
1502               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1503                                      ip4_header_t,
1504                                      dst_address /* changed member */ );
1505               tcp0->checksum = ip_csum_fold (sum0);
1506             }
1507           else if (udp0->checksum)
1508             {
1509               sum0 = udp0->checksum;
1510               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1511                                      ip4_header_t,
1512                                      dst_address /* changed member */ );
1513               udp0->checksum = ip_csum_fold (sum0);
1514             }
1515         }
1516
1517     trace00:
1518
1519       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1520                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1521         {
1522           snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1523           t->sw_if_index = sw_if_index0;
1524           t->next_index = next0;
1525         }
1526
1527       if (next0 == SNAT_OUT2IN_NEXT_DROP)
1528         {
1529           vlib_increment_simple_counter (&sm->counters.fastpath.out2in.drops,
1530                                          vm->thread_index, sw_if_index0, 1);
1531         }
1532
1533       n_left_from--;
1534       next[0] = next0;
1535       next++;
1536     }
1537
1538   vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1539                                frame->n_vectors);
1540
1541   return frame->n_vectors;
1542 }
1543
1544 /* *INDENT-OFF* */
1545 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
1546   .name = "nat44-out2in-fast",
1547   .vector_size = sizeof (u32),
1548   .format_trace = format_snat_out2in_fast_trace,
1549   .type = VLIB_NODE_TYPE_INTERNAL,
1550
1551   .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1552   .error_strings = snat_out2in_error_strings,
1553
1554   .runtime_data_bytes = sizeof (snat_runtime_t),
1555
1556   .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1557
1558   /* edit / add dispositions here */
1559   .next_nodes = {
1560     [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1561     [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1562     [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1563   },
1564 };
1565 /* *INDENT-ON* */
1566
1567 /*
1568  * fd.io coding-style-patch-verification: ON
1569  *
1570  * Local Variables:
1571  * eval: (c-set-style "gnu")
1572  * End:
1573  */