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