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, "Wireguard 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,
96 WG_INPUT_NEXT_IP6_INPUT,
103 /* set_peer_address (wg_peer_t * peer, ip4_address_t ip4, u16 udp_port) */
107 /* ip46_address_set_ip4 (&peer->dst.addr, &ip4); */
108 /* peer->dst.port = udp_port; */
113 is_ip4_header (u8 *data)
115 return (data[0] >> 4) == 0x4;
118 static wg_input_error_t
119 wg_handshake_process (vlib_main_t *vm, wg_main_t *wmp, vlib_buffer_t *b,
120 u32 node_idx, u8 is_ip4)
122 ASSERT (vm->thread_index == 0);
124 enum cookie_mac_state mac_state;
125 bool packet_needs_cookie;
129 wg_peer_t *peer = NULL;
131 void *current_b_data = vlib_buffer_get_current (b);
133 ip46_address_t src_ip;
137 current_b_data - sizeof (udp_header_t) - sizeof (ip4_header_t);
138 ip46_address_set_ip4 (&src_ip, &iph4->src_address);
143 current_b_data - sizeof (udp_header_t) - sizeof (ip6_header_t);
144 ip46_address_set_ip6 (&src_ip, &iph6->src_address);
147 udp_header_t *uhd = current_b_data - sizeof (udp_header_t);
148 u16 udp_src_port = clib_host_to_net_u16 (uhd->src_port);;
149 u16 udp_dst_port = clib_host_to_net_u16 (uhd->dst_port);;
151 message_header_t *header = current_b_data;
154 if (PREDICT_FALSE (header->type == MESSAGE_HANDSHAKE_COOKIE))
156 message_handshake_cookie_t *packet =
157 (message_handshake_cookie_t *) current_b_data;
159 wg_index_table_lookup (&wmp->index_table, packet->receiver_index);
161 peer = wg_peer_get (*entry);
163 return WG_INPUT_ERROR_PEER;
165 // TODO: Implement cookie_maker_consume_payload
167 return WG_INPUT_ERROR_NONE;
170 u32 len = (header->type == MESSAGE_HANDSHAKE_INITIATION ?
171 sizeof (message_handshake_initiation_t) :
172 sizeof (message_handshake_response_t));
174 message_macs_t *macs = (message_macs_t *)
175 ((u8 *) current_b_data + len - sizeof (*macs));
178 wg_ifs = wg_if_indexes_get_by_port (udp_dst_port);
180 return WG_INPUT_ERROR_INTERFACE;
182 vec_foreach (ii, wg_ifs)
184 wg_if = wg_if_get (*ii);
188 mac_state = cookie_checker_validate_macs (
189 vm, &wg_if->cookie_checker, macs, current_b_data, len, under_load,
190 &src_ip, udp_src_port);
191 if (mac_state == INVALID_MAC)
200 return WG_INPUT_ERROR_HANDSHAKE_MAC;
202 if ((under_load && mac_state == VALID_MAC_WITH_COOKIE)
203 || (!under_load && mac_state == VALID_MAC_BUT_NO_COOKIE))
204 packet_needs_cookie = false;
205 else if (under_load && mac_state == VALID_MAC_BUT_NO_COOKIE)
206 packet_needs_cookie = true;
208 return WG_INPUT_ERROR_HANDSHAKE_MAC;
210 switch (header->type)
212 case MESSAGE_HANDSHAKE_INITIATION:
214 message_handshake_initiation_t *message = current_b_data;
216 if (packet_needs_cookie)
218 // TODO: Add processing
221 if (noise_consume_initiation
222 (vm, noise_local_get (wg_if->local_idx), &rp,
223 message->sender_index, message->unencrypted_ephemeral,
224 message->encrypted_static, message->encrypted_timestamp))
226 peer = wg_peer_get (rp->r_peer_idx);
230 return WG_INPUT_ERROR_PEER;
233 // set_peer_address (peer, ip4_src, udp_src_port);
234 if (PREDICT_FALSE (!wg_send_handshake_response (vm, peer)))
236 vlib_node_increment_counter (vm, node_idx,
237 WG_INPUT_ERROR_HANDSHAKE_SEND, 1);
241 case MESSAGE_HANDSHAKE_RESPONSE:
243 message_handshake_response_t *resp = current_b_data;
245 wg_index_table_lookup (&wmp->index_table, resp->receiver_index);
247 if (PREDICT_TRUE (entry != NULL))
249 peer = wg_peer_get (*entry);
251 return WG_INPUT_ERROR_PEER;
254 return WG_INPUT_ERROR_PEER;
256 if (!noise_consume_response
257 (vm, &peer->remote, resp->sender_index,
258 resp->receiver_index, resp->unencrypted_ephemeral,
259 resp->encrypted_nothing))
261 return WG_INPUT_ERROR_PEER;
263 if (packet_needs_cookie)
265 // TODO: Add processing
268 // set_peer_address (peer, ip4_src, udp_src_port);
269 if (noise_remote_begin_session (vm, &peer->remote))
272 wg_timers_session_derived (peer);
273 wg_timers_handshake_complete (peer);
274 if (PREDICT_FALSE (!wg_send_keepalive (vm, peer)))
276 vlib_node_increment_counter (vm, node_idx,
277 WG_INPUT_ERROR_KEEPALIVE_SEND, 1);
283 return WG_INPUT_ERROR_HANDSHAKE_RECEIVE;
286 wg_timers_any_authenticated_packet_received (peer);
287 wg_timers_any_authenticated_packet_traversal (peer);
288 return WG_INPUT_ERROR_NONE;
292 wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
293 vlib_frame_t *frame, u8 is_ip4)
295 message_type_t header_type;
298 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
299 u16 nexts[VLIB_FRAME_SIZE], *next;
300 u32 thread_index = vm->thread_index;
302 from = vlib_frame_vector_args (frame);
303 n_left_from = frame->n_vectors;
307 vlib_get_buffers (vm, from, bufs, n_left_from);
309 wg_main_t *wmp = &wg_main;
310 wg_peer_t *peer = NULL;
312 while (n_left_from > 0)
314 bool is_keepalive = false;
315 next[0] = WG_INPUT_NEXT_PUNT;
317 ((message_header_t *) vlib_buffer_get_current (b[0]))->type;
320 if (PREDICT_TRUE (header_type == MESSAGE_DATA))
322 message_data_t *data = vlib_buffer_get_current (b[0]);
324 peer_idx = wg_index_table_lookup (&wmp->index_table,
325 data->receiver_index);
329 peer = wg_peer_get (*peer_idx);
333 next[0] = WG_INPUT_NEXT_ERROR;
334 b[0]->error = node->errors[WG_INPUT_ERROR_PEER];
338 if (PREDICT_FALSE (~0 == peer->input_thread_index))
340 /* this is the first packet to use this peer, claim the peer
343 clib_atomic_cmp_and_swap (&peer->input_thread_index, ~0,
344 wg_peer_assign_thread (thread_index));
347 if (PREDICT_TRUE (thread_index != peer->input_thread_index))
349 next[0] = WG_INPUT_NEXT_HANDOFF_DATA;
353 u16 encr_len = b[0]->current_length - sizeof (message_data_t);
354 u16 decr_len = encr_len - NOISE_AUTHTAG_LEN;
355 if (PREDICT_FALSE (decr_len >= WG_DEFAULT_DATA_SIZE))
357 b[0]->error = node->errors[WG_INPUT_ERROR_TOO_BIG];
361 u8 *decr_data = wmp->per_thread_data[thread_index].data;
363 enum noise_state_crypt state_cr = noise_remote_decrypt (vm,
365 data->receiver_index,
367 data->encrypted_data,
371 if (PREDICT_FALSE (state_cr == SC_CONN_RESET))
373 wg_timers_handshake_complete (peer);
375 else if (PREDICT_FALSE (state_cr == SC_KEEP_KEY_FRESH))
377 wg_send_handshake_from_mt (*peer_idx, false);
379 else if (PREDICT_FALSE (state_cr == SC_FAILED))
381 next[0] = WG_INPUT_NEXT_ERROR;
382 b[0]->error = node->errors[WG_INPUT_ERROR_DECRYPTION];
386 clib_memcpy (vlib_buffer_get_current (b[0]), decr_data, decr_len);
387 b[0]->current_length = decr_len;
388 vnet_buffer_offload_flags_clear (b[0],
389 VNET_BUFFER_OFFLOAD_F_UDP_CKSUM);
391 wg_timers_any_authenticated_packet_received (peer);
392 wg_timers_any_authenticated_packet_traversal (peer);
394 /* Keepalive packet has zero length */
401 wg_timers_data_received (peer);
403 ip46_address_t src_ip;
404 u8 is_ip4_inner = is_ip4_header (vlib_buffer_get_current (b[0]));
407 ip46_address_set_ip4 (
408 &src_ip, &((ip4_header_t *) vlib_buffer_get_current (b[0]))
413 ip46_address_set_ip6 (
414 &src_ip, &((ip6_header_t *) vlib_buffer_get_current (b[0]))
418 const fib_prefix_t *allowed_ip;
419 bool allowed = false;
422 * we could make this into an ACL, but the expectation
423 * is that there aren't many allowed IPs and thus a linear
424 * walk is fater than an ACL
427 vec_foreach (allowed_ip, peer->allowed_ips)
429 if (fib_prefix_is_cover_addr_46 (allowed_ip, &src_ip))
437 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = peer->wg_sw_if_index;
438 next[0] = is_ip4_inner ? WG_INPUT_NEXT_IP4_INPUT :
439 WG_INPUT_NEXT_IP6_INPUT;
446 /* Handshake packets should be processed in main thread */
447 if (thread_index != 0)
449 next[0] = WG_INPUT_NEXT_HANDOFF_HANDSHAKE;
453 wg_input_error_t ret =
454 wg_handshake_process (vm, wmp, b[0], node->node_index, is_ip4);
455 if (ret != WG_INPUT_ERROR_NONE)
457 next[0] = WG_INPUT_NEXT_ERROR;
458 b[0]->error = node->errors[ret];
463 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
464 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
466 wg_input_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
467 t->type = header_type;
468 t->current_length = b[0]->current_length;
469 t->is_keepalive = is_keepalive;
470 t->peer = peer_idx ? *peer_idx : INDEX_INVALID;
477 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
479 return frame->n_vectors;
482 VLIB_NODE_FN (wg4_input_node)
483 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
485 return wg_input_inline (vm, node, frame, /* is_ip4 */ 1);
488 VLIB_NODE_FN (wg6_input_node)
489 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
491 return wg_input_inline (vm, node, frame, /* is_ip4 */ 0);
495 VLIB_REGISTER_NODE (wg4_input_node) =
498 .vector_size = sizeof (u32),
499 .format_trace = format_wg_input_trace,
500 .type = VLIB_NODE_TYPE_INTERNAL,
501 .n_errors = ARRAY_LEN (wg_input_error_strings),
502 .error_strings = wg_input_error_strings,
503 .n_next_nodes = WG_INPUT_N_NEXT,
504 /* edit / add dispositions here */
506 [WG_INPUT_NEXT_HANDOFF_HANDSHAKE] = "wg4-handshake-handoff",
507 [WG_INPUT_NEXT_HANDOFF_DATA] = "wg4-input-data-handoff",
508 [WG_INPUT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
509 [WG_INPUT_NEXT_IP6_INPUT] = "ip6-input",
510 [WG_INPUT_NEXT_PUNT] = "error-punt",
511 [WG_INPUT_NEXT_ERROR] = "error-drop",
515 VLIB_REGISTER_NODE (wg6_input_node) =
518 .vector_size = sizeof (u32),
519 .format_trace = format_wg_input_trace,
520 .type = VLIB_NODE_TYPE_INTERNAL,
521 .n_errors = ARRAY_LEN (wg_input_error_strings),
522 .error_strings = wg_input_error_strings,
523 .n_next_nodes = WG_INPUT_N_NEXT,
524 /* edit / add dispositions here */
526 [WG_INPUT_NEXT_HANDOFF_HANDSHAKE] = "wg6-handshake-handoff",
527 [WG_INPUT_NEXT_HANDOFF_DATA] = "wg6-input-data-handoff",
528 [WG_INPUT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
529 [WG_INPUT_NEXT_IP6_INPUT] = "ip6-input",
530 [WG_INPUT_NEXT_PUNT] = "error-punt",
531 [WG_INPUT_NEXT_ERROR] = "error-drop",
537 * fd.io coding-style-patch-verification: ON
540 * eval: (c-set-style "gnu")