NAT64: Add NAT64 support for snat plugin (VPP-699)
[vpp.git] / src / plugins / snat / nat64_out2in.c
1 /*
2  * Copyright (c) 2017 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 NAT64 IPv4 to IPv6 translation (otside to inside network)
18  */
19
20 #include <snat/nat64.h>
21 #include <vnet/ip/ip4_to_ip6.h>
22 #include <vnet/fib/ip4_fib.h>
23
24 /* *INDENT-OFF* */
25 static u8 well_known_prefix[] = {
26   0x00, 0x64, 0xff, 0x9b,
27   0x00, 0x00, 0x00, 0x00,
28   0x00, 0x00, 0x00, 0x00,
29   0x00, 0x00, 0x00, 0x00
30  };
31 /* *INDENT-ON* */
32 typedef struct
33 {
34   u32 sw_if_index;
35   u32 next_index;
36 } nat64_out2in_trace_t;
37
38 static u8 *
39 format_nat64_out2in_trace (u8 * s, va_list * args)
40 {
41   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
42   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
43   nat64_out2in_trace_t *t = va_arg (*args, nat64_out2in_trace_t *);
44
45   s =
46     format (s, "NAT64-out2in: sw_if_index %d, next index %d", t->sw_if_index,
47             t->next_index);
48
49   return s;
50 }
51
52 vlib_node_registration_t nat64_out2in_node;
53
54 #define foreach_nat64_out2in_error                 \
55 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol")    \
56 _(OUT2IN_PACKETS, "Good out2in packets processed") \
57 _(NO_TRANSLATION, "No translation")                \
58 _(UNKNOWN, "unknown")
59
60 typedef enum
61 {
62 #define _(sym,str) NAT64_OUT2IN_ERROR_##sym,
63   foreach_nat64_out2in_error
64 #undef _
65     NAT64_OUT2IN_N_ERROR,
66 } nat64_out2in_error_t;
67
68 static char *nat64_out2in_error_strings[] = {
69 #define _(sym,string) string,
70   foreach_nat64_out2in_error
71 #undef _
72 };
73
74 typedef enum
75 {
76   NAT64_OUT2IN_NEXT_LOOKUP,
77   NAT64_OUT2IN_NEXT_DROP,
78   NAT64_OUT2IN_N_NEXT,
79 } nat64_out2in_next_t;
80
81 typedef struct nat64_out2in_set_ctx_t_
82 {
83   vlib_buffer_t *b;
84   vlib_main_t *vm;
85 } nat64_out2in_set_ctx_t;
86
87 static int
88 nat64_out2in_tcp_udp_set_cb (ip4_header_t * ip4, ip6_header_t * ip6,
89                              void *arg)
90 {
91   nat64_main_t *nm = &nat64_main;
92   nat64_out2in_set_ctx_t *ctx = arg;
93   nat64_db_bib_entry_t *bibe;
94   nat64_db_st_entry_t *ste;
95   ip46_address_t saddr, daddr;
96   ip6_address_t ip6_saddr;
97   udp_header_t *udp = ip4_next_header (ip4);
98   snat_protocol_t proto = ip_proto_to_snat_proto (ip4->protocol);
99   u16 dport = udp->dst_port;
100   u16 sport = udp->src_port;
101   u32 sw_if_index, fib_index;
102
103   sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
104   fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
105
106   memset (&saddr, 0, sizeof (saddr));
107   saddr.ip4.as_u32 = ip4->src_address.as_u32;
108   memset (&daddr, 0, sizeof (daddr));
109   daddr.ip4.as_u32 = ip4->dst_address.as_u32;
110
111   memcpy (&ip6_saddr, well_known_prefix, sizeof (ip6_saddr));
112   ip6_saddr.as_u32[3] = ip4->src_address.as_u32;
113
114   ste =
115     nat64_db_st_entry_find (&nm->db, &daddr, &saddr, dport, sport, proto,
116                             fib_index, 0);
117   if (ste)
118     {
119       bibe = nat64_db_bib_entry_by_index (&nm->db, proto, ste->bibe_index);
120       if (!bibe)
121         return -1;
122     }
123   else
124     {
125       bibe =
126         nat64_db_bib_entry_find (&nm->db, &daddr, dport, proto, fib_index, 0);
127
128       if (!bibe)
129         return -1;
130
131       ste =
132         nat64_db_st_entry_create (&nm->db, bibe, &ip6_saddr, &saddr.ip4,
133                                   sport);
134     }
135
136   nat64_session_reset_timeout (ste, ctx->vm);
137
138   ip6->src_address.as_u64[0] = ip6_saddr.as_u64[0];
139   ip6->src_address.as_u64[1] = ip6_saddr.as_u64[1];
140
141   ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
142   ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
143   udp->dst_port = bibe->in_port;
144
145   return 0;
146 }
147
148 static int
149 nat64_out2in_icmp_set_cb (ip4_header_t * ip4, ip6_header_t * ip6, void *arg)
150 {
151   nat64_main_t *nm = &nat64_main;
152   nat64_out2in_set_ctx_t *ctx = arg;
153   nat64_db_bib_entry_t *bibe;
154   nat64_db_st_entry_t *ste;
155   ip46_address_t saddr, daddr;
156   ip6_address_t ip6_saddr;
157   u32 sw_if_index, fib_index;
158   icmp46_header_t *icmp = ip4_next_header (ip4);
159
160   sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
161   fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
162
163   memset (&saddr, 0, sizeof (saddr));
164   saddr.ip4.as_u32 = ip4->src_address.as_u32;
165   memset (&daddr, 0, sizeof (daddr));
166   daddr.ip4.as_u32 = ip4->dst_address.as_u32;
167
168   memcpy (&ip6_saddr, well_known_prefix, sizeof (ip6_saddr));
169   ip6_saddr.as_u32[3] = ip4->src_address.as_u32;
170
171   if (icmp->type == ICMP6_echo_request || icmp->type == ICMP6_echo_reply)
172     {
173       u16 out_id = ((u16 *) (icmp))[2];
174       ste =
175         nat64_db_st_entry_find (&nm->db, &daddr, &saddr, out_id, 0,
176                                 SNAT_PROTOCOL_ICMP, fib_index, 0);
177
178       if (ste)
179         {
180           bibe =
181             nat64_db_bib_entry_by_index (&nm->db, SNAT_PROTOCOL_ICMP,
182                                          ste->bibe_index);
183           if (!bibe)
184             return -1;
185         }
186       else
187         {
188           bibe =
189             nat64_db_bib_entry_find (&nm->db, &daddr, out_id,
190                                      SNAT_PROTOCOL_ICMP, fib_index, 0);
191           if (!bibe)
192             return -1;
193
194           ste =
195             nat64_db_st_entry_create (&nm->db, bibe, &ip6_saddr, &saddr.ip4,
196                                       0);
197         }
198
199       nat64_session_reset_timeout (ste, ctx->vm);
200
201       ip6->src_address.as_u64[0] = ip6_saddr.as_u64[0];
202       ip6->src_address.as_u64[1] = ip6_saddr.as_u64[1];
203
204       ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
205       ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
206       ((u16 *) (icmp))[2] = bibe->in_port;
207
208     }
209   else
210     {
211       //TODO: ICMP error
212       clib_warning ("not ICMP echo request/reply, %u", icmp->type);
213       return -1;
214     }
215
216   return 0;
217 }
218
219 static int
220 nat64_out2in_inner_icmp_set_cb (ip4_header_t * ip4, ip6_header_t * ip6,
221                                 void *ctx)
222 {
223   //TODO:
224   return -1;
225 }
226
227 static uword
228 nat64_out2in_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
229                       vlib_frame_t * frame)
230 {
231   u32 n_left_from, *from, *to_next;
232   nat64_out2in_next_t next_index;
233   u32 pkts_processed = 0;
234
235   from = vlib_frame_vector_args (frame);
236   n_left_from = frame->n_vectors;
237   next_index = node->cached_next_index;
238   while (n_left_from > 0)
239     {
240       u32 n_left_to_next;
241
242       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
243
244       while (n_left_from > 0 && n_left_to_next > 0)
245         {
246           u32 bi0;
247           vlib_buffer_t *b0;
248           u32 next0;
249           ip4_header_t *ip40;
250           u32 proto0;
251           nat64_out2in_set_ctx_t ctx0;
252
253           /* speculatively enqueue b0 to the current next frame */
254           bi0 = from[0];
255           to_next[0] = bi0;
256           from += 1;
257           to_next += 1;
258           n_left_from -= 1;
259           n_left_to_next -= 1;
260
261           b0 = vlib_get_buffer (vm, bi0);
262           ip40 = vlib_buffer_get_current (b0);
263
264           ctx0.b = b0;
265           ctx0.vm = vm;
266
267           next0 = NAT64_OUT2IN_NEXT_LOOKUP;
268
269           proto0 = ip_proto_to_snat_proto (ip40->protocol);
270           if (PREDICT_FALSE (proto0 == ~0))
271             {
272               next0 = NAT64_OUT2IN_NEXT_DROP;
273               b0->error =
274                 node->errors[NAT64_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
275               goto trace0;
276             }
277
278           if (proto0 == SNAT_PROTOCOL_ICMP)
279             {
280               if (icmp_to_icmp6
281                   (b0, nat64_out2in_icmp_set_cb, &ctx0,
282                    nat64_out2in_inner_icmp_set_cb, &ctx0))
283                 {
284                   next0 = NAT64_OUT2IN_NEXT_DROP;
285                   b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
286                   goto trace0;
287                 }
288             }
289           else
290             {
291               if (ip4_to_ip6_tcp_udp (b0, nat64_out2in_tcp_udp_set_cb, &ctx0))
292                 {
293                   next0 = NAT64_OUT2IN_NEXT_DROP;
294                   b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
295                   goto trace0;
296                 }
297             }
298
299         trace0:
300           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
301                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
302             {
303               nat64_out2in_trace_t *t =
304                 vlib_add_trace (vm, node, b0, sizeof (*t));
305               t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
306               t->next_index = next0;
307             }
308
309           pkts_processed += next0 != NAT64_OUT2IN_NEXT_DROP;
310
311           /* verify speculative enqueue, maybe switch current next frame */
312           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
313                                            n_left_to_next, bi0, next0);
314         }
315       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
316     }
317   vlib_node_increment_counter (vm, nat64_out2in_node.index,
318                                NAT64_OUT2IN_ERROR_OUT2IN_PACKETS,
319                                pkts_processed);
320   return frame->n_vectors;
321 }
322
323 /* *INDENT-OFF* */
324 VLIB_REGISTER_NODE (nat64_out2in_node) = {
325   .function = nat64_out2in_node_fn,
326   .name = "nat64-out2in",
327   .vector_size = sizeof (u32),
328   .format_trace = format_nat64_out2in_trace,
329   .type = VLIB_NODE_TYPE_INTERNAL,
330   .n_errors = ARRAY_LEN (nat64_out2in_error_strings),
331   .error_strings = nat64_out2in_error_strings,.n_next_nodes = 2,
332   /* edit / add dispositions here */
333   .next_nodes = {
334     [NAT64_OUT2IN_NEXT_DROP] = "error-drop",
335     [NAT64_OUT2IN_NEXT_LOOKUP] = "ip6-lookup",
336   },
337 };
338 /* *INDENT-ON* */
339
340 VLIB_NODE_FUNCTION_MULTIARCH (nat64_out2in_node, nat64_out2in_node_fn);
341
342 /*
343  * fd.io coding-style-patch-verification: ON
344  *
345  * Local Variables:
346  * eval: (c-set-style "gnu")
347  * End:
348  */