wireguard: add ipv6 support
[vpp.git] / src / plugins / wireguard / wireguard_output_tun.c
1 /*
2  * Copyright (c) 2020 Doc.ai and/or its affiliates.
3  * Copyright (c) 2020 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vppinfra/error.h>
20
21 #include <wireguard/wireguard.h>
22 #include <wireguard/wireguard_send.h>
23
24 #define foreach_wg_output_error                                         \
25  _(NONE, "No error")                                                    \
26  _(PEER, "Peer error")                                                  \
27  _(KEYPAIR, "Keypair error")                                            \
28  _(TOO_BIG, "packet too big")                                           \
29
30 typedef enum
31 {
32 #define _(sym,str) WG_OUTPUT_ERROR_##sym,
33   foreach_wg_output_error
34 #undef _
35     WG_OUTPUT_N_ERROR,
36 } wg_output_error_t;
37
38 static char *wg_output_error_strings[] = {
39 #define _(sym,string) string,
40   foreach_wg_output_error
41 #undef _
42 };
43
44 typedef enum
45 {
46   WG_OUTPUT_NEXT_ERROR,
47   WG_OUTPUT_NEXT_HANDOFF,
48   WG_OUTPUT_NEXT_INTERFACE_OUTPUT,
49   WG_OUTPUT_N_NEXT,
50 } wg_output_next_t;
51
52 typedef struct
53 {
54   index_t peer;
55   u8 header[sizeof (ip6_udp_header_t)];
56   u8 is_ip4;
57 } wg_output_tun_trace_t;
58
59 u8 *
60 format_ip4_udp_header (u8 * s, va_list * args)
61 {
62   ip4_udp_header_t *hdr4 = va_arg (*args, ip4_udp_header_t *);
63
64   s = format (s, "%U:$U", format_ip4_header, &hdr4->ip4, format_udp_header,
65               &hdr4->udp);
66   return (s);
67 }
68
69 u8 *
70 format_ip6_udp_header (u8 *s, va_list *args)
71 {
72   ip6_udp_header_t *hdr6 = va_arg (*args, ip6_udp_header_t *);
73
74   s = format (s, "%U:$U", format_ip6_header, &hdr6->ip6, format_udp_header,
75               &hdr6->udp);
76   return (s);
77 }
78
79 /* packet trace format function */
80 static u8 *
81 format_wg_output_tun_trace (u8 * s, va_list * args)
82 {
83   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
84   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
85
86   wg_output_tun_trace_t *t = va_arg (*args, wg_output_tun_trace_t *);
87
88   s = format (s, "peer: %d\n", t->peer);
89   s = format (s, "  Encrypted packet: ");
90
91   s = t->is_ip4 ? format (s, "%U", format_ip4_udp_header, t->header) :
92                   format (s, "%U", format_ip6_udp_header, t->header);
93   return s;
94 }
95
96 /* is_ip4 - inner header flag */
97 always_inline uword
98 wg_output_tun_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
99                       vlib_frame_t *frame, u8 is_ip4)
100 {
101   u32 n_left_from;
102   u32 *from;
103   ip4_udp_header_t *hdr4_out = NULL;
104   ip6_udp_header_t *hdr6_out = NULL;
105   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
106   u16 nexts[VLIB_FRAME_SIZE], *next;
107   u32 thread_index = vm->thread_index;
108
109   from = vlib_frame_vector_args (frame);
110   n_left_from = frame->n_vectors;
111   b = bufs;
112   next = nexts;
113
114   vlib_get_buffers (vm, from, bufs, n_left_from);
115
116   wg_main_t *wmp = &wg_main;
117   wg_peer_t *peer = NULL;
118
119   while (n_left_from > 0)
120     {
121       index_t peeri;
122       u8 iph_offset = 0;
123       u8 is_ip4_out = 1;
124       u8 *plain_data;
125       u16 plain_data_len;
126
127       next[0] = WG_OUTPUT_NEXT_ERROR;
128       peeri =
129         wg_peer_get_by_adj_index (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
130       peer = wg_peer_get (peeri);
131
132       if (!peer || peer->is_dead)
133         {
134           b[0]->error = node->errors[WG_OUTPUT_ERROR_PEER];
135           goto out;
136         }
137       if (PREDICT_FALSE (~0 == peer->output_thread_index))
138         {
139           /* this is the first packet to use this peer, claim the peer
140            * for this thread.
141            */
142           clib_atomic_cmp_and_swap (&peer->output_thread_index, ~0,
143                                     wg_peer_assign_thread (thread_index));
144         }
145
146       if (PREDICT_TRUE (thread_index != peer->output_thread_index))
147         {
148           next[0] = WG_OUTPUT_NEXT_HANDOFF;
149           goto next;
150         }
151
152       if (PREDICT_FALSE (!peer->remote.r_current))
153         {
154           wg_send_handshake_from_mt (peeri, false);
155           b[0]->error = node->errors[WG_OUTPUT_ERROR_KEYPAIR];
156           goto out;
157         }
158
159       is_ip4_out = ip46_address_is_ip4 (&peer->src.addr);
160       if (is_ip4_out)
161         {
162           hdr4_out = vlib_buffer_get_current (b[0]);
163         }
164       else
165         {
166           hdr6_out = vlib_buffer_get_current (b[0]);
167         }
168
169       iph_offset = vnet_buffer (b[0])->ip.save_rewrite_length;
170       plain_data = vlib_buffer_get_current (b[0]) + iph_offset;
171       plain_data_len = vlib_buffer_length_in_chain (vm, b[0]) - iph_offset;
172
173       size_t encrypted_packet_len = message_data_len (plain_data_len);
174
175       /*
176        * Ensure there is enough space to write the encrypted data
177        * into the packet
178        */
179       if (PREDICT_FALSE (encrypted_packet_len >= WG_DEFAULT_DATA_SIZE) ||
180           PREDICT_FALSE ((b[0]->current_data + encrypted_packet_len) >=
181                          vlib_buffer_get_default_data_size (vm)))
182         {
183           b[0]->error = node->errors[WG_OUTPUT_ERROR_TOO_BIG];
184           goto out;
185         }
186
187       message_data_t *encrypted_packet =
188         (message_data_t *) wmp->per_thread_data[thread_index].data;
189
190       enum noise_state_crypt state;
191       state = noise_remote_encrypt (
192         vm, &peer->remote, &encrypted_packet->receiver_index,
193         &encrypted_packet->counter, plain_data, plain_data_len,
194         encrypted_packet->encrypted_data);
195
196       if (PREDICT_FALSE (state == SC_KEEP_KEY_FRESH))
197         {
198           wg_send_handshake_from_mt (peeri, false);
199         }
200       else if (PREDICT_FALSE (state == SC_FAILED))
201         {
202           //TODO: Maybe wrong
203           wg_send_handshake_from_mt (peeri, false);
204           goto out;
205         }
206
207       /* Here we are sure that can send packet to next node */
208       next[0] = WG_OUTPUT_NEXT_INTERFACE_OUTPUT;
209       encrypted_packet->header.type = MESSAGE_DATA;
210
211       clib_memcpy (plain_data, (u8 *) encrypted_packet, encrypted_packet_len);
212
213       if (is_ip4_out)
214         {
215           hdr4_out->udp.length = clib_host_to_net_u16 (encrypted_packet_len +
216                                                        sizeof (udp_header_t));
217           b[0]->current_length =
218             (encrypted_packet_len + sizeof (ip4_udp_header_t));
219           ip4_header_set_len_w_chksum (
220             &hdr4_out->ip4, clib_host_to_net_u16 (b[0]->current_length));
221         }
222       else
223         {
224           hdr6_out->udp.length = clib_host_to_net_u16 (encrypted_packet_len +
225                                                        sizeof (udp_header_t));
226           b[0]->current_length =
227             (encrypted_packet_len + sizeof (ip6_udp_header_t));
228           hdr6_out->ip6.payload_length =
229             clib_host_to_net_u16 (b[0]->current_length);
230         }
231
232       wg_timers_any_authenticated_packet_sent (peer);
233       wg_timers_data_sent (peer);
234       wg_timers_any_authenticated_packet_traversal (peer);
235
236     out:
237       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
238                          && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
239         {
240           wg_output_tun_trace_t *t =
241             vlib_add_trace (vm, node, b[0], sizeof (*t));
242
243           t->peer = peeri;
244           t->is_ip4 = is_ip4_out;
245           if (hdr4_out)
246             clib_memcpy (t->header, hdr4_out, sizeof (*hdr4_out));
247           else if (hdr6_out)
248             clib_memcpy (t->header, hdr6_out, sizeof (*hdr6_out));
249         }
250
251     next:
252       n_left_from -= 1;
253       next += 1;
254       b += 1;
255     }
256
257   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
258   return frame->n_vectors;
259 }
260
261 VLIB_NODE_FN (wg4_output_tun_node)
262 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
263 {
264   return wg_output_tun_inline (vm, node, frame, /* is_ip4 */ 1);
265 }
266
267 VLIB_NODE_FN (wg6_output_tun_node)
268 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
269 {
270   return wg_output_tun_inline (vm, node, frame, /* is_ip4 */ 0);
271 }
272
273 /* *INDENT-OFF* */
274 VLIB_REGISTER_NODE (wg4_output_tun_node) =
275 {
276   .name = "wg4-output-tun",
277   .vector_size = sizeof (u32),
278   .format_trace = format_wg_output_tun_trace,
279   .type = VLIB_NODE_TYPE_INTERNAL,
280   .n_errors = ARRAY_LEN (wg_output_error_strings),
281   .error_strings = wg_output_error_strings,
282   .n_next_nodes = WG_OUTPUT_N_NEXT,
283   .next_nodes = {
284         [WG_OUTPUT_NEXT_HANDOFF] = "wg4-output-tun-handoff",
285         [WG_OUTPUT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
286         [WG_OUTPUT_NEXT_ERROR] = "error-drop",
287   },
288 };
289
290 VLIB_REGISTER_NODE (wg6_output_tun_node) =
291 {
292   .name = "wg6-output-tun",
293   .vector_size = sizeof (u32),
294   .format_trace = format_wg_output_tun_trace,
295   .type = VLIB_NODE_TYPE_INTERNAL,
296   .n_errors = ARRAY_LEN (wg_output_error_strings),
297   .error_strings = wg_output_error_strings,
298   .n_next_nodes = WG_OUTPUT_N_NEXT,
299   .next_nodes = {
300         [WG_OUTPUT_NEXT_HANDOFF] = "wg6-output-tun-handoff",
301         [WG_OUTPUT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
302         [WG_OUTPUT_NEXT_ERROR] = "error-drop",
303   },
304 };
305 /* *INDENT-ON* */
306
307 /*
308  * fd.io coding-style-patch-verification: ON
309  *
310  * Local Variables:
311  * eval: (c-set-style "gnu")
312  * End:
313  */