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