NAT66 1:1 mapping (VPP-1108)
[vpp.git] / src / plugins / nat / nat66_out2in.c
1 /*
2  * Copyright (c) 2018 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 NAT66 outside to inside network translation
18  */
19
20 #include <nat/nat66.h>
21 #include <vnet/ip/ip6_to_ip4.h>
22 #include <vnet/fib/fib_table.h>
23
24 typedef struct
25 {
26   u32 sw_if_index;
27   u32 next_index;
28 } nat66_out2in_trace_t;
29
30 static u8 *
31 format_nat66_out2in_trace (u8 * s, va_list * args)
32 {
33   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
34   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
35   nat66_out2in_trace_t *t = va_arg (*args, nat66_out2in_trace_t *);
36
37   s =
38     format (s, "NAT66-out2in: sw_if_index %d, next index %d", t->sw_if_index,
39             t->next_index);
40
41   return s;
42 }
43
44 vlib_node_registration_t nat66_out2in_node;
45
46 #define foreach_nat66_out2in_error                       \
47 _(OUT2IN_PACKETS, "good out2in packets processed")       \
48 _(NO_TRANSLATION, "no translation")                      \
49 _(UNKNOWN, "unknown")
50
51 typedef enum
52 {
53 #define _(sym,str) NAT66_OUT2IN_ERROR_##sym,
54   foreach_nat66_out2in_error
55 #undef _
56     NAT66_OUT2IN_N_ERROR,
57 } nat66_out2in_error_t;
58
59 static char *nat66_out2in_error_strings[] = {
60 #define _(sym,string) string,
61   foreach_nat66_out2in_error
62 #undef _
63 };
64
65 typedef enum
66 {
67   NAT66_OUT2IN_NEXT_IP6_LOOKUP,
68   NAT66_OUT2IN_NEXT_DROP,
69   NAT66_OUT2IN_N_NEXT,
70 } nat66_out2in_next_t;
71
72 static inline uword
73 nat66_out2in_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
74                       vlib_frame_t * frame)
75 {
76   u32 n_left_from, *from, *to_next;
77   nat66_out2in_next_t next_index;
78   u32 pkts_processed = 0;
79   u32 thread_index = vlib_get_thread_index ();
80   nat66_main_t *nm = &nat66_main;
81
82   from = vlib_frame_vector_args (frame);
83   n_left_from = frame->n_vectors;
84   next_index = node->cached_next_index;
85
86   while (n_left_from > 0)
87     {
88       u32 n_left_to_next;
89
90       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
91
92       while (n_left_from > 0 && n_left_to_next > 0)
93         {
94           u32 bi0;
95           vlib_buffer_t *b0;
96           u32 next0 = NAT66_OUT2IN_NEXT_IP6_LOOKUP;
97           ip6_header_t *ip60;
98           u16 l4_offset0, frag_offset0;
99           u8 l4_protocol0;
100           nat66_static_mapping_t *sm0;
101           u32 sw_if_index0, fib_index0;
102           udp_header_t *udp0;
103           tcp_header_t *tcp0;
104           icmp46_header_t *icmp0;
105           u16 *checksum0 = 0;
106           ip_csum_t csum0;
107
108           /* speculatively enqueue b0 to the current next frame */
109           bi0 = from[0];
110           to_next[0] = bi0;
111           from += 1;
112           to_next += 1;
113           n_left_from -= 1;
114           n_left_to_next -= 1;
115
116           b0 = vlib_get_buffer (vm, bi0);
117           ip60 = vlib_buffer_get_current (b0);
118
119           if (PREDICT_FALSE
120               (ip6_parse
121                (ip60, b0->current_length, &l4_protocol0, &l4_offset0,
122                 &frag_offset0)))
123             {
124               next0 = NAT66_OUT2IN_NEXT_DROP;
125               b0->error = node->errors[NAT66_OUT2IN_ERROR_UNKNOWN];
126               goto trace0;
127             }
128
129           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
130           fib_index0 =
131             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6,
132                                                  sw_if_index0);
133
134           sm0 = nat66_static_mapping_get (&ip60->dst_address, fib_index0, 0);
135           if (PREDICT_FALSE (!sm0))
136             {
137               goto trace0;
138             }
139
140           if (l4_protocol0 == IP_PROTOCOL_UDP)
141             {
142               udp0 = (udp_header_t *) u8_ptr_add (ip60, l4_offset0);
143               checksum0 = &udp0->checksum;
144             }
145           else if (l4_protocol0 == IP_PROTOCOL_TCP)
146             {
147               tcp0 = (tcp_header_t *) u8_ptr_add (ip60, l4_offset0);
148               checksum0 = &tcp0->checksum;
149             }
150           else if (l4_protocol0 == IP_PROTOCOL_ICMP6)
151             {
152               icmp0 = (icmp46_header_t *) u8_ptr_add (ip60, l4_offset0);
153               checksum0 = &icmp0->checksum;
154             }
155           else
156             goto skip_csum0;
157
158           csum0 = ip_csum_sub_even (*checksum0, ip60->dst_address.as_u64[0]);
159           csum0 = ip_csum_sub_even (csum0, ip60->dst_address.as_u64[1]);
160           csum0 = ip_csum_add_even (csum0, sm0->l_addr.as_u64[0]);
161           csum0 = ip_csum_add_even (csum0, sm0->l_addr.as_u64[1]);
162           *checksum0 = ip_csum_fold (csum0);
163
164         skip_csum0:
165           ip60->dst_address.as_u64[0] = sm0->l_addr.as_u64[0];
166           ip60->dst_address.as_u64[1] = sm0->l_addr.as_u64[1];
167           vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0->fib_index;
168
169           vlib_increment_combined_counter (&nm->session_counters,
170                                            thread_index, sm0 - nm->sm, 1,
171                                            vlib_buffer_length_in_chain (vm,
172                                                                         b0));
173
174         trace0:
175           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
176                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
177             {
178               nat66_out2in_trace_t *t =
179                 vlib_add_trace (vm, node, b0, sizeof (*t));
180               t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
181               t->next_index = next0;
182             }
183
184           pkts_processed += next0 != NAT66_OUT2IN_NEXT_DROP;
185
186           /* verify speculative enqueue, maybe switch current next frame */
187           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
188                                            n_left_to_next, bi0, next0);
189         }
190       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
191     }
192
193   vlib_node_increment_counter (vm, nat66_out2in_node.index,
194                                NAT66_OUT2IN_ERROR_OUT2IN_PACKETS,
195                                pkts_processed);
196   return frame->n_vectors;
197 }
198
199 /* *INDENT-OFF* */
200 VLIB_REGISTER_NODE (nat66_out2in_node) = {
201   .function = nat66_out2in_node_fn,
202   .name = "nat66-out2in",
203   .vector_size = sizeof (u32),
204   .format_trace = format_nat66_out2in_trace,
205   .type = VLIB_NODE_TYPE_INTERNAL,
206   .n_errors = ARRAY_LEN (nat66_out2in_error_strings),
207   .error_strings = nat66_out2in_error_strings,
208   .n_next_nodes = NAT66_OUT2IN_N_NEXT,
209   /* edit / add dispositions here */
210   .next_nodes = {
211     [NAT66_OUT2IN_NEXT_DROP] = "error-drop",
212     [NAT66_OUT2IN_NEXT_IP6_LOOKUP] = "ip6-lookup",
213   },
214 };
215 /* *INDENT-ON* */
216
217 VLIB_NODE_FUNCTION_MULTIARCH (nat66_out2in_node, nat66_out2in_node_fn);
218
219 /*
220  * fd.io coding-style-patch-verification: ON
221  *
222  * Local Variables:
223  * eval: (c-set-style "gnu")
224  * End:
225  */