nat: use SVR
[vpp.git] / src / plugins / nat / nat66_in2out.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 inside to outside 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_in2out_trace_t;
29
30 static u8 *
31 format_nat66_in2out_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_in2out_trace_t *t = va_arg (*args, nat66_in2out_trace_t *);
36
37   s =
38     format (s, "NAT66-in2out: sw_if_index %d, next index %d", t->sw_if_index,
39             t->next_index);
40
41   return s;
42 }
43
44 #define foreach_nat66_in2out_error                       \
45 _(IN2OUT_PACKETS, "good in2out packets processed")       \
46 _(NO_TRANSLATION, "no translation")                      \
47 _(UNKNOWN, "unknown")
48
49 typedef enum
50 {
51 #define _(sym,str) NAT66_IN2OUT_ERROR_##sym,
52   foreach_nat66_in2out_error
53 #undef _
54     NAT66_IN2OUT_N_ERROR,
55 } nat66_in2out_error_t;
56
57 static char *nat66_in2out_error_strings[] = {
58 #define _(sym,string) string,
59   foreach_nat66_in2out_error
60 #undef _
61 };
62
63 typedef enum
64 {
65   NAT66_IN2OUT_NEXT_IP6_LOOKUP,
66   NAT66_IN2OUT_NEXT_DROP,
67   NAT66_IN2OUT_N_NEXT,
68 } nat66_in2out_next_t;
69
70 static inline u8
71 nat66_not_translate (u32 rx_fib_index, ip6_address_t ip6_addr)
72 {
73   nat66_main_t *nm = &nat66_main;
74   u32 sw_if_index;
75   snat_interface_t *i;
76   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
77   fib_prefix_t pfx = {
78     .fp_proto = FIB_PROTOCOL_IP6,
79     .fp_len = 128,
80     .fp_addr = {
81                 .ip6 = ip6_addr,
82                 },
83   };
84
85   fei = fib_table_lookup (rx_fib_index, &pfx);
86   if (FIB_NODE_INDEX_INVALID == fei)
87     return 1;
88   sw_if_index = fib_entry_get_resolving_interface (fei);
89
90   if (sw_if_index == ~0)
91     {
92       fei = fib_table_lookup (nm->outside_fib_index, &pfx);
93       if (FIB_NODE_INDEX_INVALID == fei)
94         return 1;
95       sw_if_index = fib_entry_get_resolving_interface (fei);
96     }
97
98   /* *INDENT-OFF* */
99   pool_foreach (i, nm->interfaces,
100   ({
101     /* NAT packet aimed at outside interface */
102     if (nat_interface_is_outside (i) && sw_if_index == i->sw_if_index)
103       return 0;
104   }));
105   /* *INDENT-ON* */
106
107   return 1;
108 }
109
110 VLIB_NODE_FN (nat66_in2out_node) (vlib_main_t * vm,
111                                   vlib_node_runtime_t * node,
112                                   vlib_frame_t * frame)
113 {
114   u32 n_left_from, *from, *to_next;
115   nat66_in2out_next_t next_index;
116   u32 pkts_processed = 0;
117   u32 thread_index = vm->thread_index;
118   nat66_main_t *nm = &nat66_main;
119
120   from = vlib_frame_vector_args (frame);
121   n_left_from = frame->n_vectors;
122   next_index = node->cached_next_index;
123
124   while (n_left_from > 0)
125     {
126       u32 n_left_to_next;
127
128       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
129
130       while (n_left_from > 0 && n_left_to_next > 0)
131         {
132           u32 bi0;
133           vlib_buffer_t *b0;
134           u32 next0 = NAT66_IN2OUT_NEXT_IP6_LOOKUP;
135           ip6_header_t *ip60;
136           u16 l4_offset0, frag_offset0;
137           u8 l4_protocol0;
138           nat66_static_mapping_t *sm0;
139           u32 sw_if_index0, fib_index0;
140           udp_header_t *udp0;
141           tcp_header_t *tcp0;
142           icmp46_header_t *icmp0;
143           u16 *checksum0 = 0;
144           ip_csum_t csum0;
145
146           /* speculatively enqueue b0 to the current next frame */
147           bi0 = from[0];
148           to_next[0] = bi0;
149           from += 1;
150           to_next += 1;
151           n_left_from -= 1;
152           n_left_to_next -= 1;
153
154           b0 = vlib_get_buffer (vm, bi0);
155           ip60 = vlib_buffer_get_current (b0);
156
157           if (PREDICT_FALSE
158               (ip6_parse
159                (vm, b0, ip60, b0->current_length, &l4_protocol0, &l4_offset0,
160                 &frag_offset0)))
161             {
162               next0 = NAT66_IN2OUT_NEXT_DROP;
163               b0->error = node->errors[NAT66_IN2OUT_ERROR_UNKNOWN];
164               goto trace0;
165             }
166
167           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
168           fib_index0 =
169             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6,
170                                                  sw_if_index0);
171
172           if (nat66_not_translate (fib_index0, ip60->dst_address))
173             goto trace0;
174
175           sm0 = nat66_static_mapping_get (&ip60->src_address, fib_index0, 1);
176           if (PREDICT_FALSE (!sm0))
177             {
178               goto trace0;
179             }
180
181           if (l4_protocol0 == IP_PROTOCOL_UDP)
182             {
183               udp0 = (udp_header_t *) u8_ptr_add (ip60, l4_offset0);
184               checksum0 = &udp0->checksum;
185             }
186           else if (l4_protocol0 == IP_PROTOCOL_TCP)
187             {
188               tcp0 = (tcp_header_t *) u8_ptr_add (ip60, l4_offset0);
189               checksum0 = &tcp0->checksum;
190             }
191           else if (l4_protocol0 == IP_PROTOCOL_ICMP6)
192             {
193               icmp0 = (icmp46_header_t *) u8_ptr_add (ip60, l4_offset0);
194               checksum0 = &icmp0->checksum;
195             }
196           else
197             goto skip_csum0;
198
199           csum0 = ip_csum_sub_even (*checksum0, ip60->src_address.as_u64[0]);
200           csum0 = ip_csum_sub_even (csum0, ip60->src_address.as_u64[1]);
201           csum0 = ip_csum_add_even (csum0, sm0->e_addr.as_u64[0]);
202           csum0 = ip_csum_add_even (csum0, sm0->e_addr.as_u64[1]);
203           *checksum0 = ip_csum_fold (csum0);
204
205         skip_csum0:
206           ip60->src_address.as_u64[0] = sm0->e_addr.as_u64[0];
207           ip60->src_address.as_u64[1] = sm0->e_addr.as_u64[1];
208
209           vlib_increment_combined_counter (&nm->session_counters,
210                                            thread_index, sm0 - nm->sm, 1,
211                                            vlib_buffer_length_in_chain (vm,
212                                                                         b0));
213
214         trace0:
215           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
216                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
217             {
218               nat66_in2out_trace_t *t =
219                 vlib_add_trace (vm, node, b0, sizeof (*t));
220               t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
221               t->next_index = next0;
222             }
223
224           pkts_processed += next0 != NAT66_IN2OUT_NEXT_DROP;
225
226           /* verify speculative enqueue, maybe switch current next frame */
227           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
228                                            n_left_to_next, bi0, next0);
229         }
230       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
231     }
232
233   vlib_node_increment_counter (vm, nm->in2out_node_index,
234                                NAT66_IN2OUT_ERROR_IN2OUT_PACKETS,
235                                pkts_processed);
236   return frame->n_vectors;
237 }
238
239 /* *INDENT-OFF* */
240 VLIB_REGISTER_NODE (nat66_in2out_node) = {
241   .name = "nat66-in2out",
242   .vector_size = sizeof (u32),
243   .format_trace = format_nat66_in2out_trace,
244   .type = VLIB_NODE_TYPE_INTERNAL,
245   .n_errors = ARRAY_LEN (nat66_in2out_error_strings),
246   .error_strings = nat66_in2out_error_strings,
247   .n_next_nodes = NAT66_IN2OUT_N_NEXT,
248   /* edit / add dispositions here */
249   .next_nodes = {
250     [NAT66_IN2OUT_NEXT_DROP] = "error-drop",
251     [NAT66_IN2OUT_NEXT_IP6_LOOKUP] = "ip6-lookup",
252   },
253 };
254 /* *INDENT-ON* */
255
256 /*
257  * fd.io coding-style-patch-verification: ON
258  *
259  * Local Variables:
260  * eval: (c-set-style "gnu")
261  * End:
262  */