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