2 #include <vnet/adj/adj_midchain.h>
3 #include <vnet/udp/udp.h>
5 #include <wireguard/wireguard_messages.h>
6 #include <wireguard/wireguard_if.h>
7 #include <wireguard/wireguard.h>
8 #include <wireguard/wireguard_peer.h>
10 /* pool of interfaces */
13 /* bitmap of Allocated WG_ITF instances */
14 static uword *wg_if_instances;
16 /* vector of interfaces key'd on their sw_if_index */
17 static index_t *wg_if_index_by_sw_if_index;
19 /* vector of interfaces key'd on their UDP port (in network order) */
20 index_t *wg_if_index_by_port;
23 format_wg_if_name (u8 * s, va_list * args)
25 u32 dev_instance = va_arg (*args, u32);
26 return format (s, "wg%d", dev_instance);
30 format_wg_if (u8 * s, va_list * args)
32 index_t wgii = va_arg (*args, u32);
33 wg_if_t *wgi = wg_if_get (wgii);
34 noise_local_t *local = noise_local_get (wgi->local_idx);
35 u8 key[NOISE_KEY_LEN_BASE64];
38 s = format (s, "[%d] %U src:%U port:%d",
40 format_vnet_sw_if_index_name, vnet_get_main (),
41 wgi->sw_if_index, format_ip_address, &wgi->src_ip, wgi->port);
43 key_to_base64 (local->l_private, NOISE_PUBLIC_KEY_LEN, key);
45 s = format (s, " private-key:%s", key);
47 format (s, " %U", format_hex_bytes, local->l_private,
48 NOISE_PUBLIC_KEY_LEN);
50 key_to_base64 (local->l_public, NOISE_PUBLIC_KEY_LEN, key);
52 s = format (s, " public-key:%s", key);
55 format (s, " %U", format_hex_bytes, local->l_public,
56 NOISE_PUBLIC_KEY_LEN);
58 s = format (s, " mac-key: %U", format_hex_bytes,
59 &wgi->cookie_checker.cc_mac1_key, NOISE_PUBLIC_KEY_LEN);
65 wg_if_find_by_sw_if_index (u32 sw_if_index)
67 if (vec_len (wg_if_index_by_sw_if_index) <= sw_if_index)
69 u32 ti = wg_if_index_by_sw_if_index[sw_if_index];
77 wg_if_find_peer_by_public_key (index_t peeri, void *data)
79 uint8_t *public = data;
80 wg_peer_t *peer = wg_peer_get (peeri);
82 if (!memcmp (peer->remote.r_public, public, NOISE_PUBLIC_KEY_LEN))
84 return (WALK_CONTINUE);
87 static noise_remote_t *
88 wg_remote_get (const uint8_t public[NOISE_PUBLIC_KEY_LEN])
92 peeri = wg_peer_walk (wg_if_find_peer_by_public_key, (void *) public);
94 if (INDEX_INVALID != peeri)
95 return &wg_peer_get (peeri)->remote;
101 wg_index_set (noise_remote_t * remote)
103 wg_main_t *wmp = &wg_main;
104 u32 rnd_seed = (u32) (vlib_time_now (wmp->vlib_main) * 1e6);
106 wg_index_table_add (&wmp->index_table, remote->r_peer_idx, rnd_seed);
111 wg_index_drop (uint32_t key)
113 wg_main_t *wmp = &wg_main;
114 wg_index_table_del (&wmp->index_table, key);
117 static clib_error_t *
118 wg_if_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
120 vnet_hw_interface_t *hi;
124 hi = vnet_get_hw_interface (vnm, hw_if_index);
125 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ?
126 VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
127 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
129 wgii = wg_if_find_by_sw_if_index (hi->sw_if_index);
131 wg_if_peer_walk (wg_if_get (wgii), wg_peer_if_admin_state_change, NULL);
137 wg_if_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
139 /* The peers manage the adjacencies */
144 VNET_DEVICE_CLASS (wg_if_device_class) = {
145 .name = "Wireguard Tunnel",
146 .format_device_name = format_wg_if_name,
147 .admin_up_down_function = wg_if_admin_up_down,
150 VNET_HW_INTERFACE_CLASS(wg_hw_interface_class) = {
152 .update_adjacency = wg_if_update_adj,
153 .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA,
158 * Maintain a bitmap of allocated wg_if instance numbers.
160 #define WG_ITF_MAX_INSTANCE (16 * 1024)
163 wg_if_instance_alloc (u32 want)
166 * Check for dynamically allocated instance number.
172 bit = clib_bitmap_first_clear (wg_if_instances);
173 if (bit >= WG_ITF_MAX_INSTANCE)
177 wg_if_instances = clib_bitmap_set (wg_if_instances, bit, 1);
184 if (want >= WG_ITF_MAX_INSTANCE)
192 if (clib_bitmap_get (wg_if_instances, want))
198 * Grant allocation request.
200 wg_if_instances = clib_bitmap_set (wg_if_instances, want, 1);
206 wg_if_instance_free (u32 instance)
208 if (instance >= WG_ITF_MAX_INSTANCE)
213 if (clib_bitmap_get (wg_if_instances, instance) == 0)
218 wg_if_instances = clib_bitmap_set (wg_if_instances, instance, 0);
224 wg_if_create (u32 user_instance,
225 const u8 private_key[NOISE_PUBLIC_KEY_LEN],
226 u16 port, const ip_address_t * src_ip, u32 * sw_if_indexp)
228 vnet_main_t *vnm = vnet_get_main ();
229 u32 instance, hw_if_index;
230 vnet_hw_interface_t *hi;
232 noise_local_t *local;
234 ASSERT (sw_if_indexp);
236 *sw_if_indexp = (u32) ~ 0;
239 * Allocate a wg_if instance. Either select on dynamically
240 * or try to use the desired user_instance number.
242 instance = wg_if_instance_alloc (user_instance);
244 return VNET_API_ERROR_INVALID_REGISTRATION;
247 struct noise_upcall upcall = {
248 .u_remote_get = wg_remote_get,
249 .u_index_set = wg_index_set,
250 .u_index_drop = wg_index_drop,
254 pool_get (noise_local_pool, local);
256 noise_local_init (local, &upcall);
257 if (!noise_local_set_private (local, private_key))
259 pool_put (noise_local_pool, local);
260 wg_if_instance_free (instance);
261 return VNET_API_ERROR_INVALID_REGISTRATION;
264 pool_get (wg_if_pool, wg_if);
266 /* tunnel index (or instance) */
267 u32 t_idx = wg_if - wg_if_pool;
269 wg_if->user_instance = instance;
270 if (~0 == wg_if->user_instance)
271 wg_if->user_instance = t_idx;
273 udp_register_dst_port (vlib_get_main (), port, wg_input_node.index, 1);
275 vec_validate_init_empty (wg_if_index_by_port, port, INDEX_INVALID);
276 wg_if_index_by_port[port] = wg_if - wg_if_pool;
279 wg_if->local_idx = local - noise_local_pool;
280 cookie_checker_update (&wg_if->cookie_checker, local->l_public);
282 hw_if_index = vnet_register_interface (vnm,
283 wg_if_device_class.index,
285 wg_hw_interface_class.index, t_idx);
287 hi = vnet_get_hw_interface (vnm, hw_if_index);
289 vec_validate_init_empty (wg_if_index_by_sw_if_index, hi->sw_if_index,
291 wg_if_index_by_sw_if_index[hi->sw_if_index] = t_idx;
293 ip_address_copy (&wg_if->src_ip, src_ip);
294 wg_if->sw_if_index = *sw_if_indexp = hi->sw_if_index;
300 wg_if_delete (u32 sw_if_index)
302 vnet_main_t *vnm = vnet_get_main ();
304 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
305 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
307 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
308 if (hw == 0 || hw->dev_class_index != wg_if_device_class.index)
309 return VNET_API_ERROR_INVALID_VALUE;
312 wg_if = wg_if_get (wg_if_find_by_sw_if_index (sw_if_index));
314 return VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
316 if (wg_if_instance_free (wg_if->user_instance) < 0)
317 return VNET_API_ERROR_INVALID_VALUE_2;
319 udp_unregister_dst_port (vlib_get_main (), wg_if->port, 1);
320 wg_if_index_by_port[wg_if->port] = INDEX_INVALID;
321 vnet_delete_hw_interface (vnm, hw->hw_if_index);
322 pool_put_index (noise_local_pool, wg_if->local_idx);
323 pool_put (wg_if_pool, wg_if);
329 wg_if_peer_add (wg_if_t * wgi, index_t peeri)
331 hash_set (wgi->peers, peeri, peeri);
333 if (1 == hash_elts (wgi->peers))
334 vnet_feature_enable_disable ("ip4-output", "wg-output-tun",
335 wgi->sw_if_index, 1, 0, 0);
339 wg_if_peer_remove (wg_if_t * wgi, index_t peeri)
341 hash_unset (wgi->peers, peeri);
343 if (0 == hash_elts (wgi->peers))
344 vnet_feature_enable_disable ("ip4-output", "wg-output-tun",
345 wgi->sw_if_index, 0, 0, 0);
349 wg_if_walk (wg_if_walk_cb_t fn, void *data)
354 pool_foreach_index (wgii, wg_if_pool,
356 if (WALK_STOP == fn(wgii, data))
363 wg_if_peer_walk (wg_if_t * wgi, wg_if_peer_walk_cb_t fn, void *data)
368 hash_foreach (peeri, val, wgi->peers,
370 if (WALK_STOP == fn(wgi, peeri, data))
375 return INDEX_INVALID;
380 wg_if_table_bind_v4 (ip4_main_t * im,
382 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
386 wg_if = wg_if_get (wg_if_find_by_sw_if_index (sw_if_index));
390 wg_peer_table_bind_ctx_t ctx = {
392 .old_fib_index = old_fib_index,
393 .new_fib_index = new_fib_index,
396 wg_if_peer_walk (wg_if, wg_peer_if_table_change, &ctx);
400 wg_if_table_bind_v6 (ip6_main_t * im,
402 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
406 wg_if = wg_if_get (wg_if_find_by_sw_if_index (sw_if_index));
410 wg_peer_table_bind_ctx_t ctx = {
412 .old_fib_index = old_fib_index,
413 .new_fib_index = new_fib_index,
416 wg_if_peer_walk (wg_if, wg_peer_if_table_change, &ctx);
419 static clib_error_t *
420 wg_if_module_init (vlib_main_t * vm)
423 ip4_table_bind_callback_t cb = {
424 .function = wg_if_table_bind_v4,
426 vec_add1 (ip4_main.table_bind_callbacks, cb);
429 ip6_table_bind_callback_t cb = {
430 .function = wg_if_table_bind_v6,
432 vec_add1 (ip6_main.table_bind_callbacks, cb);
439 VLIB_INIT_FUNCTION (wg_if_module_init) =
441 .runs_after = VLIB_INITS("ip_main_init"),
447 * fd.io coding-style-patch-verification: ON
450 * eval: (c-set-style "gnu")