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/adj/adj_midchain.h>
18 #include <vnet/fib/fib_table.h>
19 #include <wireguard/wireguard_peer.h>
20 #include <wireguard/wireguard_if.h>
21 #include <wireguard/wireguard_messages.h>
22 #include <wireguard/wireguard_key.h>
23 #include <wireguard/wireguard_send.h>
24 #include <wireguard/wireguard.h>
25 #include <vnet/tunnel/tunnel_dp.h>
27 wg_peer_t *wg_peer_pool;
29 index_t *wg_peer_by_adj_index;
32 wg_peer_endpoint_reset (wg_peer_endpoint_t * ep)
34 ip46_address_reset (&ep->addr);
39 wg_peer_endpoint_init (wg_peer_endpoint_t *ep, const ip46_address_t *addr,
42 ip46_address_copy (&ep->addr, addr);
47 wg_peer_clear (vlib_main_t * vm, wg_peer_t * peer)
49 wg_timers_stop (peer);
50 for (int i = 0; i < WG_N_TIMERS; i++)
53 peer->timers_dispatched[i] = 0;
56 peer->last_sent_handshake = vlib_time_now (vm) - (REKEY_TIMEOUT + 1);
58 clib_memset (&peer->cookie_maker, 0, sizeof (peer->cookie_maker));
60 wg_peer_endpoint_reset (&peer->src);
61 wg_peer_endpoint_reset (&peer->dst);
63 adj_index_t *adj_index;
64 vec_foreach (adj_index, peer->adj_indices)
66 if (INDEX_INVALID != *adj_index)
68 wg_peer_by_adj_index[*adj_index] = INDEX_INVALID;
71 peer->input_thread_index = ~0;
72 peer->output_thread_index = ~0;
73 peer->timer_wheel = 0;
74 peer->persistent_keepalive_interval = 0;
75 peer->timer_handshake_attempts = 0;
76 peer->last_sent_packet = 0;
77 peer->last_received_packet = 0;
78 peer->session_derived = 0;
79 peer->rehandshake_started = 0;
80 peer->new_handshake_interval_tick = 0;
81 peer->rehandshake_interval_tick = 0;
82 peer->timer_need_another_keepalive = false;
84 vec_free (peer->allowed_ips);
85 vec_free (peer->adj_indices);
89 wg_peer_init (vlib_main_t * vm, wg_peer_t * peer)
91 wg_peer_clear (vm, peer);
95 wg_peer_build_rewrite (const wg_peer_t *peer, u8 is_ip4)
100 ip4_udp_header_t *hdr;
102 vec_validate (rewrite, sizeof (*hdr) - 1);
103 hdr = (ip4_udp_header_t *) rewrite;
105 hdr->ip4.ip_version_and_header_length = 0x45;
107 hdr->ip4.src_address = peer->src.addr.ip4;
108 hdr->ip4.dst_address = peer->dst.addr.ip4;
109 hdr->ip4.protocol = IP_PROTOCOL_UDP;
110 hdr->ip4.checksum = ip4_header_checksum (&hdr->ip4);
112 hdr->udp.src_port = clib_host_to_net_u16 (peer->src.port);
113 hdr->udp.dst_port = clib_host_to_net_u16 (peer->dst.port);
114 hdr->udp.checksum = 0;
118 ip6_udp_header_t *hdr;
120 vec_validate (rewrite, sizeof (*hdr) - 1);
121 hdr = (ip6_udp_header_t *) rewrite;
123 hdr->ip6.ip_version_traffic_class_and_flow_label = 0x60;
124 ip6_address_copy (&hdr->ip6.src_address, &peer->src.addr.ip6);
125 ip6_address_copy (&hdr->ip6.dst_address, &peer->dst.addr.ip6);
126 hdr->ip6.protocol = IP_PROTOCOL_UDP;
127 hdr->ip6.hop_limit = 64;
129 hdr->udp.src_port = clib_host_to_net_u16 (peer->src.port);
130 hdr->udp.dst_port = clib_host_to_net_u16 (peer->dst.port);
131 hdr->udp.checksum = 0;
138 wg_peer_adj_stack (wg_peer_t *peer, adj_index_t ai)
143 fib_protocol_t fib_proto;
145 if (!adj_is_valid (ai))
149 sw_if_index = adj->rewrite_header.sw_if_index;
150 u8 is_ip4 = ip46_address_is_ip4 (&peer->src.addr);
151 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
153 wgi = wg_if_get (wg_if_find_by_sw_if_index (sw_if_index));
158 if (!vnet_sw_interface_is_admin_up (vnet_get_main (), wgi->sw_if_index))
160 adj_midchain_delegate_unstack (ai);
166 .fp_len = is_ip4 ? 32 : 128,
167 .fp_proto = fib_proto,
168 .fp_addr = peer->dst.addr,
173 fib_index = fib_table_find (fib_proto, peer->table_id);
175 adj_midchain_delegate_stack (ai, fib_index, &dst);
180 wg_peer_66_fixup (vlib_main_t *vm, const ip_adjacency_t *adj, vlib_buffer_t *b,
184 ip6_header_t *ip6_out;
185 ip6_header_t *ip6_in;
187 /* Must set locally originated otherwise we're not allowed to
188 fragment the packet later */
189 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
191 ip6_out = vlib_buffer_get_current (b);
192 iph_offset = vnet_buffer (b)->ip.save_rewrite_length;
193 ip6_in = vlib_buffer_get_current (b) + iph_offset;
195 ip6_out->ip_version_traffic_class_and_flow_label =
196 ip6_in->ip_version_traffic_class_and_flow_label;
200 wg_peer_46_fixup (vlib_main_t *vm, const ip_adjacency_t *adj, vlib_buffer_t *b,
204 ip6_header_t *ip6_out;
205 ip4_header_t *ip4_in;
207 /* Must set locally originated otherwise we're not allowed to
208 fragment the packet later */
209 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
211 ip6_out = vlib_buffer_get_current (b);
212 iph_offset = vnet_buffer (b)->ip.save_rewrite_length;
213 ip4_in = vlib_buffer_get_current (b) + iph_offset;
215 u32 vtcfl = 0x6 << 28;
216 vtcfl |= ip4_in->tos << 20;
217 vtcfl |= vnet_buffer (b)->ip.flow_hash & 0x000fffff;
219 ip6_out->ip_version_traffic_class_and_flow_label =
220 clib_host_to_net_u32 (vtcfl);
223 static adj_midchain_fixup_t
224 wg_peer_get_fixup (wg_peer_t *peer, vnet_link_t lt)
226 if (!ip46_address_is_ip4 (&peer->dst.addr))
228 if (lt == VNET_LINK_IP4)
229 return (wg_peer_46_fixup);
230 if (lt == VNET_LINK_IP6)
231 return (wg_peer_66_fixup);
237 wg_peer_if_admin_state_change (index_t peeri, void *data)
240 adj_index_t *adj_index;
241 peer = wg_peer_get (peeri);
242 vec_foreach (adj_index, peer->adj_indices)
244 wg_peer_adj_stack (peer, *adj_index);
246 return (WALK_CONTINUE);
250 wg_peer_if_adj_change (index_t peeri, void *data)
252 adj_index_t *adj_index = data;
253 adj_midchain_fixup_t fixup;
256 fib_prefix_t *allowed_ip;
258 adj = adj_get (*adj_index);
260 peer = wg_peer_get (peeri);
261 vec_foreach (allowed_ip, peer->allowed_ips)
263 if (fib_prefix_is_cover_addr_46 (allowed_ip,
264 &adj->sub_type.nbr.next_hop))
266 vec_add1 (peer->adj_indices, *adj_index);
267 vec_validate_init_empty (wg_peer_by_adj_index, *adj_index,
269 wg_peer_by_adj_index[*adj_index] = peer - wg_peer_pool;
271 fixup = wg_peer_get_fixup (peer, adj_get_link_type (*adj_index));
272 adj_nbr_midchain_update_rewrite (*adj_index, fixup, NULL,
273 ADJ_FLAG_MIDCHAIN_IP_STACK,
274 vec_dup (peer->rewrite));
276 wg_peer_adj_stack (peer, *adj_index);
281 return (WALK_CONTINUE);
285 wg_peer_adj_walk (adj_index_t ai, void *data)
287 return wg_peer_if_adj_change ((*(index_t *) (data)), &ai) == WALK_CONTINUE ?
288 ADJ_WALK_RC_CONTINUE :
293 wg_peer_if_delete (index_t peeri, void *data)
295 wg_peer_remove (peeri);
296 return (WALK_CONTINUE);
300 wg_peer_fill (vlib_main_t *vm, wg_peer_t *peer, u32 table_id,
301 const ip46_address_t *dst, u16 port,
302 u16 persistent_keepalive_interval,
303 const fib_prefix_t *allowed_ips, u32 wg_sw_if_index)
305 wg_peer_endpoint_init (&peer->dst, dst, port);
307 peer->table_id = table_id;
308 peer->wg_sw_if_index = wg_sw_if_index;
309 peer->timer_wheel = &wg_main.timer_wheel;
310 peer->persistent_keepalive_interval = persistent_keepalive_interval;
311 peer->last_sent_handshake = vlib_time_now (vm) - (REKEY_TIMEOUT + 1);
312 peer->is_dead = false;
314 const wg_if_t *wgi = wg_if_get (wg_if_find_by_sw_if_index (wg_sw_if_index));
317 return (VNET_API_ERROR_INVALID_INTERFACE);
319 ip_address_to_46 (&wgi->src_ip, &peer->src.addr);
320 peer->src.port = wgi->port;
322 u8 is_ip4 = ip46_address_is_ip4 (&peer->dst.addr);
323 peer->rewrite = wg_peer_build_rewrite (peer, is_ip4);
326 vec_validate (peer->allowed_ips, vec_len (allowed_ips) - 1);
327 vec_foreach_index (ii, allowed_ips)
329 peer->allowed_ips[ii] = allowed_ips[ii];
332 index_t perri = peer - wg_peer_pool;
333 fib_protocol_t proto;
334 FOR_EACH_FIB_IP_PROTOCOL (proto)
336 adj_nbr_walk (wg_sw_if_index, proto, wg_peer_adj_walk, &perri);
342 wg_peer_add (u32 tun_sw_if_index, const u8 public_key[NOISE_PUBLIC_KEY_LEN],
343 u32 table_id, const ip46_address_t *endpoint,
344 const fib_prefix_t *allowed_ips, u16 port,
345 u16 persistent_keepalive, u32 *peer_index)
351 vlib_main_t *vm = vlib_get_main ();
353 if (tun_sw_if_index == ~0)
354 return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
356 wg_if = wg_if_get (wg_if_find_by_sw_if_index (tun_sw_if_index));
358 return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
361 pool_foreach (peer, wg_peer_pool)
363 if (!memcmp (peer->remote.r_public, public_key, NOISE_PUBLIC_KEY_LEN))
365 return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
370 if (pool_elts (wg_peer_pool) > MAX_PEERS)
371 return (VNET_API_ERROR_LIMIT_EXCEEDED);
373 pool_get (wg_peer_pool, peer);
375 wg_peer_init (vm, peer);
377 rv = wg_peer_fill (vm, peer, table_id, endpoint, (u16) port,
378 persistent_keepalive, allowed_ips, tun_sw_if_index);
382 wg_peer_clear (vm, peer);
383 pool_put (wg_peer_pool, peer);
387 noise_remote_init (&peer->remote, peer - wg_peer_pool, public_key,
389 cookie_maker_init (&peer->cookie_maker, public_key);
391 if (peer->persistent_keepalive_interval != 0)
393 wg_send_keepalive (vm, peer);
396 *peer_index = peer - wg_peer_pool;
397 wg_if_peer_add (wg_if, *peer_index);
403 wg_peer_remove (index_t peeri)
405 wg_main_t *wmp = &wg_main;
406 wg_peer_t *peer = NULL;
409 if (pool_is_free_index (wg_peer_pool, peeri))
410 return VNET_API_ERROR_NO_SUCH_ENTRY;
412 peer = pool_elt_at_index (wg_peer_pool, peeri);
414 wgi = wg_if_get (wg_if_find_by_sw_if_index (peer->wg_sw_if_index));
415 wg_if_peer_remove (wgi, peeri);
417 noise_remote_clear (wmp->vlib_main, &peer->remote);
418 wg_peer_clear (wmp->vlib_main, peer);
419 pool_put (wg_peer_pool, peer);
425 wg_peer_walk (wg_peer_walk_cb_t fn, void *data)
430 pool_foreach_index (peeri, wg_peer_pool)
432 if (WALK_STOP == fn(peeri, data))
436 return INDEX_INVALID;
440 format_wg_peer_endpoint (u8 * s, va_list * args)
442 wg_peer_endpoint_t *ep = va_arg (*args, wg_peer_endpoint_t *);
444 s = format (s, "%U:%d", format_ip46_address, &ep->addr, IP46_TYPE_ANY,
451 format_wg_peer (u8 * s, va_list * va)
453 index_t peeri = va_arg (*va, index_t);
454 fib_prefix_t *allowed_ip;
455 adj_index_t *adj_index;
456 u8 key[NOISE_KEY_LEN_BASE64];
459 peer = wg_peer_get (peeri);
460 key_to_base64 (peer->remote.r_public, NOISE_PUBLIC_KEY_LEN, key);
462 s = format (s, "[%d] endpoint:[%U->%U] %U keep-alive:%d", peeri,
463 format_wg_peer_endpoint, &peer->src, format_wg_peer_endpoint,
464 &peer->dst, format_vnet_sw_if_index_name, vnet_get_main (),
465 peer->wg_sw_if_index, peer->persistent_keepalive_interval);
466 s = format (s, "\n adj:");
467 vec_foreach (adj_index, peer->adj_indices)
469 s = format (s, " %d", adj_index);
471 s = format (s, "\n key:%=s %U", key, format_hex_bytes,
472 peer->remote.r_public, NOISE_PUBLIC_KEY_LEN);
473 s = format (s, "\n allowed-ips:");
474 vec_foreach (allowed_ip, peer->allowed_ips)
476 s = format (s, " %U", format_fib_prefix, allowed_ip);
483 * fd.io coding-style-patch-verification: ON
486 * eval: (c-set-style "gnu")