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