wireguard: add events for peer
[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 (wg_peer_is_dead (peer))
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           wg_peer_update_flags (peeri, WG_PEER_ESTABLISHED, false);
205           goto out;
206         }
207
208       /* Here we are sure that can send packet to next node */
209       next[0] = WG_OUTPUT_NEXT_INTERFACE_OUTPUT;
210       encrypted_packet->header.type = MESSAGE_DATA;
211
212       clib_memcpy (plain_data, (u8 *) encrypted_packet, encrypted_packet_len);
213
214       if (is_ip4_out)
215         {
216           hdr4_out->udp.length = clib_host_to_net_u16 (encrypted_packet_len +
217                                                        sizeof (udp_header_t));
218           b[0]->current_length =
219             (encrypted_packet_len + sizeof (ip4_udp_header_t));
220           ip4_header_set_len_w_chksum (
221             &hdr4_out->ip4, clib_host_to_net_u16 (b[0]->current_length));
222         }
223       else
224         {
225           hdr6_out->udp.length = clib_host_to_net_u16 (encrypted_packet_len +
226                                                        sizeof (udp_header_t));
227           b[0]->current_length =
228             (encrypted_packet_len + sizeof (ip6_udp_header_t));
229           hdr6_out->ip6.payload_length =
230             clib_host_to_net_u16 (b[0]->current_length);
231         }
232
233       wg_timers_any_authenticated_packet_sent (peer);
234       wg_timers_data_sent (peer);
235       wg_timers_any_authenticated_packet_traversal (peer);
236
237     out:
238       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
239                          && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
240         {
241           wg_output_tun_trace_t *t =
242             vlib_add_trace (vm, node, b[0], sizeof (*t));
243
244           t->peer = peeri;
245           t->is_ip4 = is_ip4_out;
246           if (hdr4_out)
247             clib_memcpy (t->header, hdr4_out, sizeof (*hdr4_out));
248           else if (hdr6_out)
249             clib_memcpy (t->header, hdr6_out, sizeof (*hdr6_out));
250         }
251
252     next:
253       n_left_from -= 1;
254       next += 1;
255       b += 1;
256     }
257
258   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
259   return frame->n_vectors;
260 }
261
262 VLIB_NODE_FN (wg4_output_tun_node)
263 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
264 {
265   return wg_output_tun_inline (vm, node, frame, /* is_ip4 */ 1);
266 }
267
268 VLIB_NODE_FN (wg6_output_tun_node)
269 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
270 {
271   return wg_output_tun_inline (vm, node, frame, /* is_ip4 */ 0);
272 }
273
274 /* *INDENT-OFF* */
275 VLIB_REGISTER_NODE (wg4_output_tun_node) =
276 {
277   .name = "wg4-output-tun",
278   .vector_size = sizeof (u32),
279   .format_trace = format_wg_output_tun_trace,
280   .type = VLIB_NODE_TYPE_INTERNAL,
281   .n_errors = ARRAY_LEN (wg_output_error_strings),
282   .error_strings = wg_output_error_strings,
283   .n_next_nodes = WG_OUTPUT_N_NEXT,
284   .next_nodes = {
285         [WG_OUTPUT_NEXT_HANDOFF] = "wg4-output-tun-handoff",
286         [WG_OUTPUT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
287         [WG_OUTPUT_NEXT_ERROR] = "error-drop",
288   },
289 };
290
291 VLIB_REGISTER_NODE (wg6_output_tun_node) =
292 {
293   .name = "wg6-output-tun",
294   .vector_size = sizeof (u32),
295   .format_trace = format_wg_output_tun_trace,
296   .type = VLIB_NODE_TYPE_INTERNAL,
297   .n_errors = ARRAY_LEN (wg_output_error_strings),
298   .error_strings = wg_output_error_strings,
299   .n_next_nodes = WG_OUTPUT_N_NEXT,
300   .next_nodes = {
301         [WG_OUTPUT_NEXT_HANDOFF] = "wg6-output-tun-handoff",
302         [WG_OUTPUT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
303         [WG_OUTPUT_NEXT_ERROR] = "error-drop",
304   },
305 };
306 /* *INDENT-ON* */
307
308 /*
309  * fd.io coding-style-patch-verification: ON
310  *
311  * Local Variables:
312  * eval: (c-set-style "gnu")
313  * End:
314  */