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 <vnet/vnet.h>
18 #include <vnet/ip/ip6_link.h>
19 #include <vnet/udp/udp.h>
20 #include <vppinfra/error.h>
21 #include <vlibmemory/api.h>
22 #include <wireguard/wireguard.h>
23 #include <wireguard/wireguard_send.h>
26 ip46_enqueue_packet (vlib_main_t * vm, u32 bi0, int is_ip6)
29 u32 lookup_node_index =
30 is_ip6 ? ip6_lookup_node.index : ip4_lookup_node.index;
32 f = vlib_get_frame_to_node (vm, lookup_node_index);
33 /* f can not be NULL here - frame allocation failure causes panic */
35 u32 *to_next = vlib_frame_vector_args (f);
39 vlib_put_frame_to_node (vm, lookup_node_index, f);
45 wg_buffer_prepend_rewrite (vlib_buffer_t * b0, const wg_peer_t * peer)
47 ip4_udp_header_t *hdr;
49 vlib_buffer_advance (b0, -sizeof (*hdr));
51 hdr = vlib_buffer_get_current (b0);
52 clib_memcpy (hdr, peer->rewrite, vec_len (peer->rewrite));
55 clib_host_to_net_u16 (b0->current_length - sizeof (ip4_header_t));
56 ip4_header_set_len_w_chksum (&hdr->ip4,
57 clib_host_to_net_u16 (b0->current_length));
61 wg_create_buffer (vlib_main_t * vm,
62 const wg_peer_t * peer,
63 const u8 * packet, u32 packet_len, u32 * bi)
68 n_buf0 = vlib_buffer_alloc (vm, bi, 1);
72 b0 = vlib_get_buffer (vm, *bi);
74 u8 *payload = vlib_buffer_get_current (b0);
75 clib_memcpy (payload, packet, packet_len);
77 b0->current_length = packet_len;
79 wg_buffer_prepend_rewrite (b0, peer);
85 wg_send_handshake (vlib_main_t * vm, wg_peer_t * peer, bool is_retry)
87 ASSERT (vm->thread_index == 0);
89 message_handshake_initiation_t packet;
92 peer->timer_handshake_attempts = 0;
94 if (!wg_birthdate_has_expired (peer->last_sent_handshake,
95 REKEY_TIMEOUT) || peer->is_dead)
98 if (noise_create_initiation (vm,
100 &packet.sender_index,
101 packet.unencrypted_ephemeral,
102 packet.encrypted_static,
103 packet.encrypted_timestamp))
105 packet.header.type = MESSAGE_HANDSHAKE_INITIATION;
106 cookie_maker_mac (&peer->cookie_maker, &packet.macs, &packet,
108 wg_timers_any_authenticated_packet_sent (peer);
109 wg_timers_handshake_initiated (peer);
110 wg_timers_any_authenticated_packet_traversal (peer);
112 peer->last_sent_handshake = vlib_time_now (vm);
118 if (!wg_create_buffer (vm, peer, (u8 *) & packet, sizeof (packet), &bi0))
121 ip46_enqueue_packet (vm, bi0, false);
132 wg_send_handshake_thread_fn (void *arg)
134 wg_send_args_t *a = arg;
136 wg_main_t *wmp = &wg_main;
137 wg_peer_t *peer = wg_peer_get (a->peer_idx);
139 wg_send_handshake (wmp->vlib_main, peer, a->is_retry);
144 wg_send_handshake_from_mt (u32 peer_idx, bool is_retry)
147 .peer_idx = peer_idx,
148 .is_retry = is_retry,
151 vl_api_rpc_call_main_thread (wg_send_handshake_thread_fn,
152 (u8 *) & a, sizeof (a));
156 wg_send_keepalive (vlib_main_t * vm, wg_peer_t * peer)
158 ASSERT (vm->thread_index == 0);
160 u32 size_of_packet = message_data_len (0);
161 message_data_t *packet =
162 (message_data_t *) wg_main.per_thread_data[vm->thread_index].data;
165 enum noise_state_crypt state;
167 if (!peer->remote.r_current)
169 wg_send_handshake (vm, peer, false);
174 noise_remote_encrypt (vm,
176 &packet->receiver_index,
177 &packet->counter, NULL, 0, packet->encrypted_data);
179 if (PREDICT_FALSE (state == SC_KEEP_KEY_FRESH))
181 wg_send_handshake (vm, peer, false);
183 else if (PREDICT_FALSE (state == SC_FAILED))
189 packet->header.type = MESSAGE_DATA;
191 if (!wg_create_buffer (vm, peer, (u8 *) packet, size_of_packet, &bi0))
197 ip46_enqueue_packet (vm, bi0, false);
199 wg_timers_any_authenticated_packet_sent (peer);
200 wg_timers_any_authenticated_packet_traversal (peer);
207 wg_send_handshake_response (vlib_main_t * vm, wg_peer_t * peer)
209 message_handshake_response_t packet;
211 if (noise_create_response (vm,
213 &packet.sender_index,
214 &packet.receiver_index,
215 packet.unencrypted_ephemeral,
216 packet.encrypted_nothing))
218 packet.header.type = MESSAGE_HANDSHAKE_RESPONSE;
219 cookie_maker_mac (&peer->cookie_maker, &packet.macs, &packet,
222 if (noise_remote_begin_session (vm, &peer->remote))
224 wg_timers_session_derived (peer);
225 wg_timers_any_authenticated_packet_sent (peer);
226 wg_timers_any_authenticated_packet_traversal (peer);
227 peer->last_sent_handshake = vlib_time_now (vm);
230 if (!wg_create_buffer (vm, peer, (u8 *) & packet,
231 sizeof (packet), &bi0))
234 ip46_enqueue_packet (vm, bi0, false);
245 * fd.io coding-style-patch-verification: ON
248 * eval: (c-set-style "gnu")