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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vppinfra/error.h>
20 #include <wireguard/wireguard.h>
22 #include <wireguard/wireguard_send.h>
23 #include <wireguard/wireguard_if.h>
25 #define foreach_wg_input_error \
26 _ (NONE, "No error") \
27 _ (HANDSHAKE_MAC, "Invalid MAC handshake") \
28 _ (PEER, "Peer error") \
29 _ (INTERFACE, "Interface error") \
30 _ (DECRYPTION, "Failed during decryption") \
31 _ (KEEPALIVE_SEND, "Failed while sending Keepalive") \
32 _ (HANDSHAKE_SEND, "Failed while sending Handshake") \
33 _ (HANDSHAKE_RECEIVE, "Failed while receiving Handshake") \
34 _ (TOO_BIG, "Packet too big") \
35 _ (UNDEFINED, "Undefined error")
39 #define _(sym,str) WG_INPUT_ERROR_##sym,
40 foreach_wg_input_error
45 static char *wg_input_error_strings[] = {
46 #define _(sym,string) string,
47 foreach_wg_input_error
60 format_wg_message_type (u8 * s, va_list * args)
62 message_type_t type = va_arg (*args, message_type_t);
66 #define _(v,a) case MESSAGE_##v: return (format (s, "%s", a));
67 foreach_wg_message_type
70 return (format (s, "unknown"));
73 /* packet trace format function */
75 format_wg_input_trace (u8 * s, va_list * args)
77 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
78 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
80 wg_input_trace_t *t = va_arg (*args, wg_input_trace_t *);
82 s = format (s, "WG input: \n");
83 s = format (s, " Type: %U\n", format_wg_message_type, t->type);
84 s = format (s, " peer: %d\n", t->peer);
85 s = format (s, " Length: %d\n", t->current_length);
86 s = format (s, " Keepalive: %s", t->is_keepalive ? "true" : "false");
93 WG_INPUT_NEXT_HANDOFF_HANDSHAKE,
94 WG_INPUT_NEXT_HANDOFF_DATA,
95 WG_INPUT_NEXT_IP4_INPUT,
102 /* set_peer_address (wg_peer_t * peer, ip4_address_t ip4, u16 udp_port) */
106 /* ip46_address_set_ip4 (&peer->dst.addr, &ip4); */
107 /* peer->dst.port = udp_port; */
111 static wg_input_error_t
112 wg_handshake_process (vlib_main_t * vm, wg_main_t * wmp, vlib_buffer_t * b)
114 ASSERT (vm->thread_index == 0);
116 enum cookie_mac_state mac_state;
117 bool packet_needs_cookie;
121 wg_peer_t *peer = NULL;
123 void *current_b_data = vlib_buffer_get_current (b);
125 udp_header_t *uhd = current_b_data - sizeof (udp_header_t);
127 current_b_data - sizeof (udp_header_t) - sizeof (ip4_header_t);
128 ip4_address_t ip4_src = iph->src_address;
129 u16 udp_src_port = clib_host_to_net_u16 (uhd->src_port);;
130 u16 udp_dst_port = clib_host_to_net_u16 (uhd->dst_port);;
132 message_header_t *header = current_b_data;
135 if (PREDICT_FALSE (header->type == MESSAGE_HANDSHAKE_COOKIE))
137 message_handshake_cookie_t *packet =
138 (message_handshake_cookie_t *) current_b_data;
140 wg_index_table_lookup (&wmp->index_table, packet->receiver_index);
142 peer = wg_peer_get (*entry);
144 return WG_INPUT_ERROR_PEER;
146 // TODO: Implement cookie_maker_consume_payload
148 return WG_INPUT_ERROR_NONE;
151 u32 len = (header->type == MESSAGE_HANDSHAKE_INITIATION ?
152 sizeof (message_handshake_initiation_t) :
153 sizeof (message_handshake_response_t));
155 message_macs_t *macs = (message_macs_t *)
156 ((u8 *) current_b_data + len - sizeof (*macs));
159 wg_ifs = wg_if_indexes_get_by_port (udp_dst_port);
161 return WG_INPUT_ERROR_INTERFACE;
163 vec_foreach (ii, wg_ifs)
165 wg_if = wg_if_get (*ii);
169 mac_state = cookie_checker_validate_macs (
170 vm, &wg_if->cookie_checker, macs, current_b_data, len, under_load,
171 ip4_src, udp_src_port);
172 if (mac_state == INVALID_MAC)
181 return WG_INPUT_ERROR_HANDSHAKE_MAC;
183 if ((under_load && mac_state == VALID_MAC_WITH_COOKIE)
184 || (!under_load && mac_state == VALID_MAC_BUT_NO_COOKIE))
185 packet_needs_cookie = false;
186 else if (under_load && mac_state == VALID_MAC_BUT_NO_COOKIE)
187 packet_needs_cookie = true;
189 return WG_INPUT_ERROR_HANDSHAKE_MAC;
191 switch (header->type)
193 case MESSAGE_HANDSHAKE_INITIATION:
195 message_handshake_initiation_t *message = current_b_data;
197 if (packet_needs_cookie)
199 // TODO: Add processing
202 if (noise_consume_initiation
203 (vm, noise_local_get (wg_if->local_idx), &rp,
204 message->sender_index, message->unencrypted_ephemeral,
205 message->encrypted_static, message->encrypted_timestamp))
207 peer = wg_peer_get (rp->r_peer_idx);
211 return WG_INPUT_ERROR_PEER;
214 // set_peer_address (peer, ip4_src, udp_src_port);
215 if (PREDICT_FALSE (!wg_send_handshake_response (vm, peer)))
217 vlib_node_increment_counter (vm, wg_input_node.index,
218 WG_INPUT_ERROR_HANDSHAKE_SEND, 1);
222 case MESSAGE_HANDSHAKE_RESPONSE:
224 message_handshake_response_t *resp = current_b_data;
226 wg_index_table_lookup (&wmp->index_table, resp->receiver_index);
228 if (PREDICT_TRUE (entry != NULL))
230 peer = wg_peer_get (*entry);
232 return WG_INPUT_ERROR_PEER;
235 return WG_INPUT_ERROR_PEER;
237 if (!noise_consume_response
238 (vm, &peer->remote, resp->sender_index,
239 resp->receiver_index, resp->unencrypted_ephemeral,
240 resp->encrypted_nothing))
242 return WG_INPUT_ERROR_PEER;
244 if (packet_needs_cookie)
246 // TODO: Add processing
249 // set_peer_address (peer, ip4_src, udp_src_port);
250 if (noise_remote_begin_session (vm, &peer->remote))
253 wg_timers_session_derived (peer);
254 wg_timers_handshake_complete (peer);
255 if (PREDICT_FALSE (!wg_send_keepalive (vm, peer)))
257 vlib_node_increment_counter (vm, wg_input_node.index,
258 WG_INPUT_ERROR_KEEPALIVE_SEND,
265 return WG_INPUT_ERROR_HANDSHAKE_RECEIVE;
268 wg_timers_any_authenticated_packet_received (peer);
269 wg_timers_any_authenticated_packet_traversal (peer);
270 return WG_INPUT_ERROR_NONE;
273 VLIB_NODE_FN (wg_input_node) (vlib_main_t * vm,
274 vlib_node_runtime_t * node,
275 vlib_frame_t * frame)
277 message_type_t header_type;
280 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
281 u16 nexts[VLIB_FRAME_SIZE], *next;
282 u32 thread_index = vm->thread_index;
284 from = vlib_frame_vector_args (frame);
285 n_left_from = frame->n_vectors;
289 vlib_get_buffers (vm, from, bufs, n_left_from);
291 wg_main_t *wmp = &wg_main;
292 wg_peer_t *peer = NULL;
294 while (n_left_from > 0)
296 bool is_keepalive = false;
297 next[0] = WG_INPUT_NEXT_PUNT;
299 ((message_header_t *) vlib_buffer_get_current (b[0]))->type;
302 if (PREDICT_TRUE (header_type == MESSAGE_DATA))
304 message_data_t *data = vlib_buffer_get_current (b[0]);
306 peer_idx = wg_index_table_lookup (&wmp->index_table,
307 data->receiver_index);
311 peer = wg_peer_get (*peer_idx);
315 next[0] = WG_INPUT_NEXT_ERROR;
316 b[0]->error = node->errors[WG_INPUT_ERROR_PEER];
320 if (PREDICT_FALSE (~0 == peer->input_thread_index))
322 /* this is the first packet to use this peer, claim the peer
325 clib_atomic_cmp_and_swap (&peer->input_thread_index, ~0,
326 wg_peer_assign_thread (thread_index));
329 if (PREDICT_TRUE (thread_index != peer->input_thread_index))
331 next[0] = WG_INPUT_NEXT_HANDOFF_DATA;
335 u16 encr_len = b[0]->current_length - sizeof (message_data_t);
336 u16 decr_len = encr_len - NOISE_AUTHTAG_LEN;
337 if (PREDICT_FALSE (decr_len >= WG_DEFAULT_DATA_SIZE))
339 b[0]->error = node->errors[WG_INPUT_ERROR_TOO_BIG];
343 u8 *decr_data = wmp->per_thread_data[thread_index].data;
345 enum noise_state_crypt state_cr = noise_remote_decrypt (vm,
347 data->receiver_index,
349 data->encrypted_data,
353 if (PREDICT_FALSE (state_cr == SC_CONN_RESET))
355 wg_timers_handshake_complete (peer);
357 else if (PREDICT_FALSE (state_cr == SC_KEEP_KEY_FRESH))
359 wg_send_handshake_from_mt (*peer_idx, false);
361 else if (PREDICT_FALSE (state_cr == SC_FAILED))
363 next[0] = WG_INPUT_NEXT_ERROR;
364 b[0]->error = node->errors[WG_INPUT_ERROR_DECRYPTION];
368 clib_memcpy (vlib_buffer_get_current (b[0]), decr_data, decr_len);
369 b[0]->current_length = decr_len;
370 vnet_buffer_offload_flags_clear (b[0],
371 VNET_BUFFER_OFFLOAD_F_UDP_CKSUM);
373 wg_timers_any_authenticated_packet_received (peer);
374 wg_timers_any_authenticated_packet_traversal (peer);
376 /* Keepalive packet has zero length */
383 wg_timers_data_received (peer);
385 ip4_header_t *iph = vlib_buffer_get_current (b[0]);
387 const fib_prefix_t *allowed_ip;
388 bool allowed = false;
391 * we could make this into an ACL, but the expectation
392 * is that there aren't many allowed IPs and thus a linear
393 * walk is fater than an ACL
395 vec_foreach (allowed_ip, peer->allowed_ips)
397 if (fib_prefix_is_cover_addr_4 (allowed_ip, &iph->src_address))
405 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = peer->wg_sw_if_index;
406 next[0] = WG_INPUT_NEXT_IP4_INPUT;
413 /* Handshake packets should be processed in main thread */
414 if (thread_index != 0)
416 next[0] = WG_INPUT_NEXT_HANDOFF_HANDSHAKE;
420 wg_input_error_t ret = wg_handshake_process (vm, wmp, b[0]);
421 if (ret != WG_INPUT_ERROR_NONE)
423 next[0] = WG_INPUT_NEXT_ERROR;
424 b[0]->error = node->errors[ret];
429 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
430 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
432 wg_input_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
433 t->type = header_type;
434 t->current_length = b[0]->current_length;
435 t->is_keepalive = is_keepalive;
436 t->peer = peer_idx ? *peer_idx : INDEX_INVALID;
443 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
445 return frame->n_vectors;
449 VLIB_REGISTER_NODE (wg_input_node) =
452 .vector_size = sizeof (u32),
453 .format_trace = format_wg_input_trace,
454 .type = VLIB_NODE_TYPE_INTERNAL,
455 .n_errors = ARRAY_LEN (wg_input_error_strings),
456 .error_strings = wg_input_error_strings,
457 .n_next_nodes = WG_INPUT_N_NEXT,
458 /* edit / add dispositions here */
460 [WG_INPUT_NEXT_HANDOFF_HANDSHAKE] = "wg-handshake-handoff",
461 [WG_INPUT_NEXT_HANDOFF_DATA] = "wg-input-data-handoff",
462 [WG_INPUT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
463 [WG_INPUT_NEXT_PUNT] = "error-punt",
464 [WG_INPUT_NEXT_ERROR] = "error-drop",
470 * fd.io coding-style-patch-verification: ON
473 * eval: (c-set-style "gnu")