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