session svm: fix fifo migration
[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
33 #include <vppinfra/hash.h>
34 #include <vppinfra/error.h>
35 #include <vppinfra/elog.h>
36
37 typedef struct
38 {
39   u32 sw_if_index;
40   u32 next_index;
41   u32 session_index;
42 } snat_out2in_trace_t;
43
44 /* packet trace format function */
45 static u8 *
46 format_snat_out2in_trace (u8 * s, va_list * args)
47 {
48   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
49   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
50   snat_out2in_trace_t *t = va_arg (*args, snat_out2in_trace_t *);
51
52   s =
53     format (s,
54             "NAT44_OUT2IN: sw_if_index %d, next index %d, session index %d",
55             t->sw_if_index, t->next_index, t->session_index);
56   return s;
57 }
58
59 static u8 *
60 format_snat_out2in_fast_trace (u8 * s, va_list * args)
61 {
62   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
63   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
64   snat_out2in_trace_t *t = va_arg (*args, snat_out2in_trace_t *);
65
66   s = format (s, "NAT44_OUT2IN_FAST: sw_if_index %d, next index %d",
67               t->sw_if_index, t->next_index);
68   return s;
69 }
70
71 #define foreach_snat_out2in_error                       \
72 _(UNSUPPORTED_PROTOCOL, "unsupported protocol")         \
73 _(OUT_OF_PORTS, "out of ports")                         \
74 _(BAD_ICMP_TYPE, "unsupported ICMP type")               \
75 _(NO_TRANSLATION, "no translation")                     \
76 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")   \
77 _(CANNOT_CREATE_USER, "cannot create NAT user")
78
79 typedef enum
80 {
81 #define _(sym,str) SNAT_OUT2IN_ERROR_##sym,
82   foreach_snat_out2in_error
83 #undef _
84     SNAT_OUT2IN_N_ERROR,
85 } snat_out2in_error_t;
86
87 static char *snat_out2in_error_strings[] = {
88 #define _(sym,string) string,
89   foreach_snat_out2in_error
90 #undef _
91 };
92
93 typedef enum
94 {
95   SNAT_OUT2IN_NEXT_DROP,
96   SNAT_OUT2IN_NEXT_LOOKUP,
97   SNAT_OUT2IN_NEXT_ICMP_ERROR,
98   SNAT_OUT2IN_N_NEXT,
99 } snat_out2in_next_t;
100
101 #ifndef CLIB_MARCH_VARIANT
102 int
103 nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
104 {
105   snat_main_t *sm = &snat_main;
106   nat44_is_idle_session_ctx_t *ctx = arg;
107   snat_session_t *s;
108   u64 sess_timeout_time;
109   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
110                                                        ctx->thread_index);
111   clib_bihash_kv_8_8_t s_kv;
112
113   s = pool_elt_at_index (tsm->sessions, kv->value);
114   sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
115   if (ctx->now >= sess_timeout_time)
116     {
117       init_nat_i2o_k (&s_kv, s);
118       if (clib_bihash_add_del_8_8 (&tsm->in2out, &s_kv, 0))
119         nat_elog_warn ("out2in key del failed");
120
121       nat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
122                                           s->in2out.addr.as_u32,
123                                           s->out2in.addr.as_u32,
124                                           s->nat_proto,
125                                           s->in2out.port,
126                                           s->out2in.port,
127                                           s->in2out.fib_index);
128
129       nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
130                                &s->in2out.addr, s->in2out.port,
131                                &s->out2in.addr, s->out2in.port, s->nat_proto);
132
133       nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
134                    s->ext_host_port, s->nat_proto, s->out2in.fib_index,
135                    ctx->thread_index);
136
137       if (!snat_is_session_static (s))
138         snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
139                                             &s->out2in.addr, s->out2in.port,
140                                             s->nat_proto);
141
142       nat44_delete_session (sm, s, ctx->thread_index);
143       return 1;
144     }
145
146   return 0;
147 }
148 #endif
149
150 /**
151  * @brief Create session for static mapping.
152  *
153  * Create NAT session initiated by host from external network with static
154  * mapping.
155  *
156  * @param sm     NAT main.
157  * @param b0     Vlib buffer.
158  * @param in2out In2out NAT44 session key.
159  * @param out2in Out2in NAT44 session key.
160  * @param node   Vlib node.
161  *
162  * @returns SNAT session if successfully created otherwise 0.
163  */
164 static inline snat_session_t *
165 create_session_for_static_mapping (snat_main_t * sm,
166                                    vlib_buffer_t * b0,
167                                    ip4_address_t i2o_addr,
168                                    u16 i2o_port,
169                                    u32 i2o_fib_index,
170                                    ip4_address_t o2i_addr,
171                                    u16 o2i_port,
172                                    u32 o2i_fib_index,
173                                    nat_protocol_t proto,
174                                    vlib_node_runtime_t * node,
175                                    u32 thread_index, f64 now)
176 {
177   snat_user_t *u;
178   snat_session_t *s;
179   clib_bihash_kv_8_8_t kv0;
180   ip4_header_t *ip0;
181   udp_header_t *udp0;
182   nat44_is_idle_session_ctx_t ctx0;
183
184   if (PREDICT_FALSE (nat44_ei_maximum_sessions_exceeded (sm, thread_index)))
185     {
186       b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_SESSIONS_EXCEEDED];
187       nat_elog_notice ("maximum sessions exceeded");
188       return 0;
189     }
190
191   ip0 = vlib_buffer_get_current (b0);
192   udp0 = ip4_next_header (ip0);
193
194   u = nat_user_get_or_create (sm, &i2o_addr, i2o_fib_index, thread_index);
195   if (!u)
196     {
197       b0->error = node->errors[SNAT_OUT2IN_ERROR_CANNOT_CREATE_USER];
198       return 0;
199     }
200
201   s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
202   if (!s)
203     {
204       nat44_delete_user_with_no_session (sm, u, thread_index);
205       nat_elog_warn ("create NAT session failed");
206       return 0;
207     }
208
209   s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
210   s->ext_host_addr.as_u32 = ip0->src_address.as_u32;
211   s->ext_host_port = udp0->src_port;
212   user_session_increment (sm, u, 1 /* static */ );
213   s->in2out.addr = i2o_addr;
214   s->in2out.port = i2o_port;
215   s->in2out.fib_index = i2o_fib_index;
216   s->out2in.addr = o2i_addr;
217   s->out2in.port = o2i_port;
218   s->out2in.fib_index = o2i_fib_index;
219   s->nat_proto = proto;
220
221   /* Add to translation hashes */
222   ctx0.now = now;
223   ctx0.thread_index = thread_index;
224   init_nat_i2o_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
225   if (clib_bihash_add_or_overwrite_stale_8_8
226       (&sm->per_thread_data[thread_index].in2out, &kv0,
227        nat44_i2o_is_idle_session_cb, &ctx0))
228     nat_elog_notice ("in2out key add failed");
229
230   init_nat_o2i_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
231   if (clib_bihash_add_or_overwrite_stale_8_8
232       (&sm->per_thread_data[thread_index].out2in, &kv0,
233        nat44_o2i_is_idle_session_cb, &ctx0))
234     nat_elog_notice ("out2in key add failed");
235
236   /* log NAT event */
237   nat_ipfix_logging_nat44_ses_create (thread_index,
238                                       s->in2out.addr.as_u32,
239                                       s->out2in.addr.as_u32,
240                                       s->nat_proto,
241                                       s->in2out.port,
242                                       s->out2in.port, s->in2out.fib_index);
243
244   nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
245                            &s->in2out.addr, s->in2out.port, &s->out2in.addr,
246                            s->out2in.port, s->nat_proto);
247
248   nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
249                s->out2in.port, &s->ext_host_addr, s->ext_host_port,
250                &s->ext_host_nat_addr, s->ext_host_nat_port,
251                s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
252
253   return s;
254 }
255
256 #ifndef CLIB_MARCH_VARIANT
257 static_always_inline snat_out2in_error_t
258 icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
259               ip4_address_t * addr, u16 * port, nat_protocol_t * nat_proto)
260 {
261   icmp46_header_t *icmp0;
262   icmp_echo_header_t *echo0, *inner_echo0 = 0;
263   ip4_header_t *inner_ip0;
264   void *l4_header = 0;
265   icmp46_header_t *inner_icmp0;
266
267   icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
268   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
269
270   if (!icmp_type_is_error_message
271       (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
272     {
273       *nat_proto = NAT_PROTOCOL_ICMP;
274       *addr = ip0->dst_address;
275       *port = vnet_buffer (b)->ip.reass.l4_src_port;
276     }
277   else
278     {
279       inner_ip0 = (ip4_header_t *) (echo0 + 1);
280       l4_header = ip4_next_header (inner_ip0);
281       *nat_proto = ip_proto_to_nat_proto (inner_ip0->protocol);
282       *addr = inner_ip0->src_address;
283       switch (*nat_proto)
284         {
285         case NAT_PROTOCOL_ICMP:
286           inner_icmp0 = (icmp46_header_t *) l4_header;
287           inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
288           *port = inner_echo0->identifier;
289           break;
290         case NAT_PROTOCOL_UDP:
291         case NAT_PROTOCOL_TCP:
292           *port = ((tcp_udp_header_t *) l4_header)->src_port;
293           break;
294         default:
295           return SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL;
296         }
297     }
298   return -1;                    /* success */
299 }
300
301 /**
302  * Get address and port values to be used for ICMP packet translation
303  * and create session if needed
304  *
305  * @param[in,out] sm             NAT main
306  * @param[in,out] node           NAT node runtime
307  * @param[in] thread_index       thread index
308  * @param[in,out] b0             buffer containing packet to be translated
309  * @param[in,out] ip0            ip header
310  * @param[out] p_proto           protocol used for matching
311  * @param[out] p_value           address and port after NAT translation
312  * @param[out] p_dont_translate  if packet should not be translated
313  * @param d                      optional parameter
314  * @param e                      optional parameter
315  */
316 u32
317 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
318                         u32 thread_index, vlib_buffer_t * b0,
319                         ip4_header_t * ip0, ip4_address_t * addr,
320                         u16 * port, u32 * fib_index,
321                         nat_protocol_t * proto, void *d, void *e,
322                         u8 * dont_translate)
323 {
324   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
325   u32 sw_if_index0;
326   snat_session_t *s0 = 0;
327   clib_bihash_kv_8_8_t kv0, value0;
328   u8 is_addr_only;
329   u32 next0 = ~0;
330   int err;
331   u8 identity_nat;
332   vlib_main_t *vm = vlib_get_main ();
333   *dont_translate = 0;
334
335   sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
336   *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
337
338   *proto = 0;
339
340   err = icmp_get_key (b0, ip0, addr, port, proto);
341   if (err != -1)
342     {
343       b0->error = node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
344       next0 = SNAT_OUT2IN_NEXT_DROP;
345       goto out;
346     }
347
348   ip4_address_t mapping_addr;
349   u16 mapping_port;
350   u32 mapping_fib_index;
351
352   init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
353   if (clib_bihash_search_8_8 (&tsm->out2in, &kv0, &value0))
354     {
355       /* Try to match static mapping by external address and port,
356          destination address and port in packet */
357       if (snat_static_mapping_match
358           (sm, *addr, *port, *fib_index, *proto,
359            &mapping_addr, &mapping_port, &mapping_fib_index, 1, &is_addr_only,
360            0, 0, 0, &identity_nat, 0))
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 (snat_static_mapping_match
485       (sm, addr, port, rx_fib_index0, *proto, mapping_addr, mapping_port,
486        mapping_fib_index, 1, &is_addr_only, 0, 0, 0, 0, 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 (snat_static_mapping_match
832               (sm, ip0->dst_address,
833                vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
834                proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1, 0, 0, 0,
835                0, &identity_nat0, 0))
836             {
837               /*
838                * Send DHCP packets to the ipv4 stack, or we won't
839                * be able to use dhcp client on the outside interface
840                */
841               if (PREDICT_FALSE
842                   (proto0 == NAT_PROTOCOL_UDP
843                    && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
844                        clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
845                 {
846                   vnet_feature_next (&next0, b0);
847                   goto trace0;
848                 }
849
850               if (!sm->forwarding_enabled)
851                 {
852                   b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
853                   next0 = SNAT_OUT2IN_NEXT_DROP;
854                 }
855               goto trace0;
856             }
857
858           if (PREDICT_FALSE (identity_nat0))
859             goto trace0;
860
861           /* Create session initiated by host from external network */
862           s0 = create_session_for_static_mapping (sm, b0,
863                                                   sm_addr0, sm_port0,
864                                                   sm_fib_index0,
865                                                   ip0->dst_address,
866                                                   vnet_buffer (b0)->ip.
867                                                   reass.l4_dst_port,
868                                                   rx_fib_index0, proto0, node,
869                                                   thread_index, now);
870           if (!s0)
871             {
872               next0 = SNAT_OUT2IN_NEXT_DROP;
873               goto trace0;
874             }
875         }
876       else
877         s0 = pool_elt_at_index (tsm->sessions, value0.value);
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
1007       if (clib_bihash_search_8_8
1008           (&sm->per_thread_data[thread_index].out2in, &kv1, &value1))
1009         {
1010           /* Try to match static mapping by external address and port,
1011              destination address and port in packet */
1012           if (snat_static_mapping_match
1013               (sm, ip1->dst_address,
1014                vnet_buffer (b1)->ip.reass.l4_dst_port, proto1,
1015                rx_fib_index1, &sm_addr1, &sm_port1, &sm_fib_index1, 1, 0,
1016                0, 0, 0, &identity_nat1, 0))
1017             {
1018               /*
1019                * Send DHCP packets to the ipv4 stack, or we won't
1020                * be able to use dhcp client on the outside interface
1021                */
1022               if (PREDICT_FALSE
1023                   (proto1 == NAT_PROTOCOL_UDP
1024                    && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1025                        clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1026                 {
1027                   vnet_feature_next (&next1, b1);
1028                   goto trace1;
1029                 }
1030
1031               if (!sm->forwarding_enabled)
1032                 {
1033                   b1->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1034                   next1 = SNAT_OUT2IN_NEXT_DROP;
1035                 }
1036               goto trace1;
1037             }
1038
1039           if (PREDICT_FALSE (identity_nat1))
1040             goto trace1;
1041
1042           /* Create session initiated by host from external network */
1043           s1 =
1044             create_session_for_static_mapping (sm, b1, sm_addr1, sm_port1,
1045                                                sm_fib_index1,
1046                                                ip1->dst_address,
1047                                                vnet_buffer (b1)->ip.
1048                                                reass.l4_dst_port,
1049                                                rx_fib_index1, proto1, node,
1050                                                thread_index, now);
1051           if (!s1)
1052             {
1053               next1 = SNAT_OUT2IN_NEXT_DROP;
1054               goto trace1;
1055             }
1056         }
1057       else
1058         s1 =
1059           pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1060                              value1.value);
1061
1062       old_addr1 = ip1->dst_address.as_u32;
1063       ip1->dst_address = s1->in2out.addr;
1064       new_addr1 = ip1->dst_address.as_u32;
1065       vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1066
1067       sum1 = ip1->checksum;
1068       sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1069                              ip4_header_t, dst_address /* changed member */ );
1070       ip1->checksum = ip_csum_fold (sum1);
1071
1072       if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1073         {
1074           if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1075             {
1076               old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1077               new_port1 = udp1->dst_port = s1->in2out.port;
1078
1079               sum1 = tcp1->checksum;
1080               sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1081                                      ip4_header_t,
1082                                      dst_address /* changed member */ );
1083
1084               sum1 = ip_csum_update (sum1, old_port1, new_port1,
1085                                      ip4_header_t /* cheat */ ,
1086                                      length /* changed member */ );
1087               tcp1->checksum = ip_csum_fold (sum1);
1088             }
1089           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1090                                          thread_index, sw_if_index1, 1);
1091         }
1092       else
1093         {
1094           if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1095             {
1096               old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1097               new_port1 = udp1->dst_port = s1->in2out.port;
1098               if (PREDICT_FALSE (udp1->checksum))
1099                 {
1100
1101                   sum1 = udp1->checksum;
1102                   sum1 =
1103                     ip_csum_update (sum1, old_addr1, new_addr1,
1104                                     ip4_header_t,
1105                                     dst_address /* changed member */ );
1106                   sum1 =
1107                     ip_csum_update (sum1, old_port1, new_port1,
1108                                     ip4_header_t /* cheat */ ,
1109                                     length /* changed member */ );
1110                   udp1->checksum = ip_csum_fold (sum1);
1111                 }
1112             }
1113           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1114                                          thread_index, sw_if_index1, 1);
1115         }
1116
1117       /* Accounting */
1118       nat44_ei_session_update_counters (
1119         s1, now, vlib_buffer_length_in_chain (vm, b1), thread_index);
1120       /* Per-user LRU list maintenance */
1121       nat44_session_update_lru (sm, s1, thread_index);
1122     trace1:
1123
1124       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1125                          && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1126         {
1127           snat_out2in_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
1128           t->sw_if_index = sw_if_index1;
1129           t->next_index = next1;
1130           t->session_index = ~0;
1131           if (s1)
1132             t->session_index =
1133               s1 - sm->per_thread_data[thread_index].sessions;
1134         }
1135
1136       if (next1 == SNAT_OUT2IN_NEXT_DROP)
1137         {
1138           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1139                                          thread_index, sw_if_index1, 1);
1140         }
1141
1142       n_left_from -= 2;
1143       next[0] = next0;
1144       next[1] = next1;
1145       next += 2;
1146     }
1147
1148   while (n_left_from > 0)
1149     {
1150       vlib_buffer_t *b0;
1151       u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1152       u32 sw_if_index0;
1153       ip4_header_t *ip0;
1154       ip_csum_t sum0;
1155       u32 new_addr0, old_addr0;
1156       u16 new_port0, old_port0;
1157       udp_header_t *udp0;
1158       tcp_header_t *tcp0;
1159       icmp46_header_t *icmp0;
1160       u32 rx_fib_index0;
1161       u32 proto0;
1162       snat_session_t *s0 = 0;
1163       clib_bihash_kv_8_8_t kv0, value0;
1164       u8 identity_nat0;
1165       ip4_address_t sm_addr0;
1166       u16 sm_port0;
1167       u32 sm_fib_index0;
1168
1169       b0 = *b;
1170       ++b;
1171
1172       vnet_buffer (b0)->snat.flags = 0;
1173
1174       ip0 = vlib_buffer_get_current (b0);
1175       udp0 = ip4_next_header (ip0);
1176       tcp0 = (tcp_header_t *) udp0;
1177       icmp0 = (icmp46_header_t *) udp0;
1178
1179       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1180       rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1181                                sw_if_index0);
1182
1183       proto0 = ip_proto_to_nat_proto (ip0->protocol);
1184
1185       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1186         {
1187           if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1188             {
1189               if (!sm->forwarding_enabled)
1190                 {
1191                   b0->error =
1192                     node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
1193                   next0 = SNAT_OUT2IN_NEXT_DROP;
1194                 }
1195             }
1196           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
1197                                          thread_index, sw_if_index0, 1);
1198           goto trace00;
1199         }
1200
1201       if (PREDICT_FALSE (ip0->ttl == 1))
1202         {
1203           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1204           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1205                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
1206                                        0);
1207           next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1208           goto trace00;
1209         }
1210
1211       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1212         {
1213           next0 = icmp_out2in_slow_path
1214             (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1215              next0, now, thread_index, &s0);
1216           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
1217                                          thread_index, sw_if_index0, 1);
1218           goto trace00;
1219         }
1220
1221       init_nat_k (&kv0, ip0->dst_address,
1222                   vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1223                   proto0);
1224
1225       if (clib_bihash_search_8_8
1226           (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
1227         {
1228           /* Try to match static mapping by external address and port,
1229              destination address and port in packet */
1230           if (snat_static_mapping_match
1231               (sm, ip0->dst_address,
1232                vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1233                proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1, 0, 0, 0,
1234                0, &identity_nat0, 0))
1235             {
1236               /*
1237                * Send DHCP packets to the ipv4 stack, or we won't
1238                * be able to use dhcp client on the outside interface
1239                */
1240               if (PREDICT_FALSE
1241                   (proto0 == NAT_PROTOCOL_UDP
1242                    && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1243                        clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1244                 {
1245                   vnet_feature_next (&next0, b0);
1246                   goto trace00;
1247                 }
1248
1249               if (!sm->forwarding_enabled)
1250                 {
1251                   b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1252                   next0 = SNAT_OUT2IN_NEXT_DROP;
1253                 }
1254               goto trace00;
1255             }
1256
1257           if (PREDICT_FALSE (identity_nat0))
1258             goto trace00;
1259
1260           /* Create session initiated by host from external network */
1261           s0 = create_session_for_static_mapping (sm, b0,
1262                                                   sm_addr0, sm_port0,
1263                                                   sm_fib_index0,
1264                                                   ip0->dst_address,
1265                                                   vnet_buffer (b0)->ip.
1266                                                   reass.l4_dst_port,
1267                                                   rx_fib_index0, proto0, node,
1268                                                   thread_index, now);
1269           if (!s0)
1270             {
1271               next0 = SNAT_OUT2IN_NEXT_DROP;
1272               goto trace00;
1273             }
1274         }
1275       else
1276         s0 =
1277           pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1278                              value0.value);
1279
1280       old_addr0 = ip0->dst_address.as_u32;
1281       ip0->dst_address = s0->in2out.addr;
1282       new_addr0 = ip0->dst_address.as_u32;
1283       vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1284
1285       sum0 = ip0->checksum;
1286       sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1287                              ip4_header_t, dst_address /* changed member */ );
1288       ip0->checksum = ip_csum_fold (sum0);
1289
1290       if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1291         {
1292           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1293             {
1294               old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1295               new_port0 = udp0->dst_port = s0->in2out.port;
1296
1297               sum0 = tcp0->checksum;
1298               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1299                                      ip4_header_t,
1300                                      dst_address /* changed member */ );
1301
1302               sum0 = ip_csum_update (sum0, old_port0, new_port0,
1303                                      ip4_header_t /* cheat */ ,
1304                                      length /* changed member */ );
1305               tcp0->checksum = ip_csum_fold (sum0);
1306             }
1307           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1308                                          thread_index, sw_if_index0, 1);
1309         }
1310       else
1311         {
1312           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1313             {
1314               old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1315               new_port0 = udp0->dst_port = s0->in2out.port;
1316               if (PREDICT_FALSE (udp0->checksum))
1317                 {
1318                   sum0 = udp0->checksum;
1319                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address  /* changed member */
1320                     );
1321                   sum0 =
1322                     ip_csum_update (sum0, old_port0, new_port0,
1323                                     ip4_header_t /* cheat */ ,
1324                                     length /* changed member */ );
1325                   udp0->checksum = ip_csum_fold (sum0);
1326                 }
1327             }
1328           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1329                                          thread_index, sw_if_index0, 1);
1330         }
1331
1332       /* Accounting */
1333       nat44_ei_session_update_counters (
1334         s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
1335       /* Per-user LRU list maintenance */
1336       nat44_session_update_lru (sm, s0, thread_index);
1337     trace00:
1338
1339       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1340                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1341         {
1342           snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1343           t->sw_if_index = sw_if_index0;
1344           t->next_index = next0;
1345           t->session_index = ~0;
1346           if (s0)
1347             t->session_index =
1348               s0 - sm->per_thread_data[thread_index].sessions;
1349         }
1350
1351       if (next0 == SNAT_OUT2IN_NEXT_DROP)
1352         {
1353           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1354                                          thread_index, sw_if_index0, 1);
1355         }
1356
1357       n_left_from--;
1358       next[0] = next0;
1359       next++;
1360     }
1361
1362   vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1363                                frame->n_vectors);
1364
1365   return frame->n_vectors;
1366 }
1367
1368 /* *INDENT-OFF* */
1369 VLIB_REGISTER_NODE (snat_out2in_node) = {
1370   .name = "nat44-out2in",
1371   .vector_size = sizeof (u32),
1372   .format_trace = format_snat_out2in_trace,
1373   .type = VLIB_NODE_TYPE_INTERNAL,
1374
1375   .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1376   .error_strings = snat_out2in_error_strings,
1377
1378   .runtime_data_bytes = sizeof (snat_runtime_t),
1379
1380   .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1381
1382   /* edit / add dispositions here */
1383   .next_nodes = {
1384     [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1385     [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1386     [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1387   },
1388 };
1389 /* *INDENT-ON* */
1390
1391 VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm,
1392                                       vlib_node_runtime_t * node,
1393                                       vlib_frame_t * frame)
1394 {
1395   u32 n_left_from, *from;
1396   snat_main_t *sm = &snat_main;
1397
1398   from = vlib_frame_vector_args (frame);
1399   n_left_from = frame->n_vectors;
1400
1401   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1402   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1403   vlib_get_buffers (vm, from, b, n_left_from);
1404   while (n_left_from > 0)
1405     {
1406       vlib_buffer_t *b0;
1407       u32 next0 = SNAT_OUT2IN_NEXT_DROP;
1408       u32 sw_if_index0;
1409       ip4_header_t *ip0;
1410       ip_csum_t sum0;
1411       u32 new_addr0, old_addr0;
1412       u16 new_port0, old_port0;
1413       udp_header_t *udp0;
1414       tcp_header_t *tcp0;
1415       icmp46_header_t *icmp0;
1416       u32 proto0;
1417       u32 rx_fib_index0;
1418       ip4_address_t sm_addr0;
1419       u16 sm_port0;
1420       u32 sm_fib_index0;
1421
1422       b0 = *b;
1423       b++;
1424
1425       ip0 = vlib_buffer_get_current (b0);
1426       udp0 = ip4_next_header (ip0);
1427       tcp0 = (tcp_header_t *) udp0;
1428       icmp0 = (icmp46_header_t *) udp0;
1429
1430       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1431       rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1432
1433       vnet_feature_next (&next0, b0);
1434
1435       if (PREDICT_FALSE (ip0->ttl == 1))
1436         {
1437           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1438           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1439                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
1440                                        0);
1441           next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1442           goto trace00;
1443         }
1444
1445       proto0 = ip_proto_to_nat_proto (ip0->protocol);
1446
1447       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1448         goto trace00;
1449
1450       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1451         {
1452           next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
1453                                rx_fib_index0, node, next0, ~0, 0, 0);
1454           goto trace00;
1455         }
1456
1457       if (snat_static_mapping_match
1458           (sm, ip0->dst_address, udp0->dst_port, rx_fib_index0, proto0,
1459            &sm_addr0, &sm_port0, &sm_fib_index0, 1, 0, 0, 0, 0, 0, 0))
1460         {
1461           b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1462           goto trace00;
1463         }
1464
1465       new_addr0 = sm_addr0.as_u32;
1466       new_port0 = sm_port0;
1467       vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm_fib_index0;
1468       old_addr0 = ip0->dst_address.as_u32;
1469       ip0->dst_address.as_u32 = new_addr0;
1470
1471       sum0 = ip0->checksum;
1472       sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1473                              ip4_header_t, dst_address /* changed member */ );
1474       ip0->checksum = ip_csum_fold (sum0);
1475
1476       if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1477         {
1478           old_port0 = udp0->dst_port;
1479           udp0->dst_port = new_port0;
1480
1481           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1482             {
1483               sum0 = tcp0->checksum;
1484               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1485                                      ip4_header_t,
1486                                      dst_address /* changed member */ );
1487               sum0 = ip_csum_update (sum0, old_port0, new_port0,
1488                                      ip4_header_t /* cheat */ ,
1489                                      length /* changed member */ );
1490               tcp0->checksum = ip_csum_fold (sum0);
1491             }
1492           else if (udp0->checksum)
1493             {
1494               sum0 = udp0->checksum;
1495               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1496                                      ip4_header_t,
1497                                      dst_address /* changed member */ );
1498               sum0 = ip_csum_update (sum0, old_port0, new_port0,
1499                                      ip4_header_t /* cheat */ ,
1500                                      length /* changed member */ );
1501               udp0->checksum = ip_csum_fold (sum0);
1502             }
1503         }
1504       else
1505         {
1506           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1507             {
1508               sum0 = tcp0->checksum;
1509               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1510                                      ip4_header_t,
1511                                      dst_address /* changed member */ );
1512               tcp0->checksum = ip_csum_fold (sum0);
1513             }
1514           else if (udp0->checksum)
1515             {
1516               sum0 = udp0->checksum;
1517               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1518                                      ip4_header_t,
1519                                      dst_address /* changed member */ );
1520               udp0->checksum = ip_csum_fold (sum0);
1521             }
1522         }
1523
1524     trace00:
1525
1526       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1527                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1528         {
1529           snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1530           t->sw_if_index = sw_if_index0;
1531           t->next_index = next0;
1532         }
1533
1534       if (next0 == SNAT_OUT2IN_NEXT_DROP)
1535         {
1536           vlib_increment_simple_counter (&sm->counters.fastpath.out2in.drops,
1537                                          vm->thread_index, sw_if_index0, 1);
1538         }
1539
1540       n_left_from--;
1541       next[0] = next0;
1542       next++;
1543     }
1544
1545   vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1546                                frame->n_vectors);
1547
1548   return frame->n_vectors;
1549 }
1550
1551 /* *INDENT-OFF* */
1552 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
1553   .name = "nat44-out2in-fast",
1554   .vector_size = sizeof (u32),
1555   .format_trace = format_snat_out2in_fast_trace,
1556   .type = VLIB_NODE_TYPE_INTERNAL,
1557
1558   .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1559   .error_strings = snat_out2in_error_strings,
1560
1561   .runtime_data_bytes = sizeof (snat_runtime_t),
1562
1563   .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1564
1565   /* edit / add dispositions here */
1566   .next_nodes = {
1567     [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1568     [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1569     [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1570   },
1571 };
1572 /* *INDENT-ON* */
1573
1574 /*
1575  * fd.io coding-style-patch-verification: ON
1576  *
1577  * Local Variables:
1578  * eval: (c-set-style "gnu")
1579  * End:
1580  */