2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Copyright (c) 2020 Doc.ai 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/udp/udp.h>
20 #include <wireguard/wireguard_messages.h>
21 #include <wireguard/wireguard_if.h>
22 #include <wireguard/wireguard.h>
23 #include <wireguard/wireguard_peer.h>
25 /* pool of interfaces */
28 /* bitmap of Allocated WG_ITF instances */
29 static uword *wg_if_instances;
31 /* vector of interfaces key'd on their sw_if_index */
32 static index_t *wg_if_index_by_sw_if_index;
34 /* vector of interfaces key'd on their UDP port (in network order) */
35 index_t *wg_if_index_by_port;
38 format_wg_if_name (u8 * s, va_list * args)
40 u32 dev_instance = va_arg (*args, u32);
41 return format (s, "wg%d", dev_instance);
45 format_wg_if (u8 * s, va_list * args)
47 index_t wgii = va_arg (*args, u32);
48 wg_if_t *wgi = wg_if_get (wgii);
49 noise_local_t *local = noise_local_get (wgi->local_idx);
50 u8 key[NOISE_KEY_LEN_BASE64];
53 s = format (s, "[%d] %U src:%U port:%d",
55 format_vnet_sw_if_index_name, vnet_get_main (),
56 wgi->sw_if_index, format_ip_address, &wgi->src_ip, wgi->port);
58 key_to_base64 (local->l_private, NOISE_PUBLIC_KEY_LEN, key);
60 s = format (s, " private-key:%s", key);
62 format (s, " %U", format_hex_bytes, local->l_private,
63 NOISE_PUBLIC_KEY_LEN);
65 key_to_base64 (local->l_public, NOISE_PUBLIC_KEY_LEN, key);
67 s = format (s, " public-key:%s", key);
70 format (s, " %U", format_hex_bytes, local->l_public,
71 NOISE_PUBLIC_KEY_LEN);
73 s = format (s, " mac-key: %U", format_hex_bytes,
74 &wgi->cookie_checker.cc_mac1_key, NOISE_PUBLIC_KEY_LEN);
80 wg_if_find_by_sw_if_index (u32 sw_if_index)
82 if (vec_len (wg_if_index_by_sw_if_index) <= sw_if_index)
84 u32 ti = wg_if_index_by_sw_if_index[sw_if_index];
92 wg_if_find_peer_by_public_key (index_t peeri, void *data)
94 uint8_t *public = data;
95 wg_peer_t *peer = wg_peer_get (peeri);
97 if (!memcmp (peer->remote.r_public, public, NOISE_PUBLIC_KEY_LEN))
99 return (WALK_CONTINUE);
102 static noise_remote_t *
103 wg_remote_get (const uint8_t public[NOISE_PUBLIC_KEY_LEN])
107 peeri = wg_peer_walk (wg_if_find_peer_by_public_key, (void *) public);
109 if (INDEX_INVALID != peeri)
110 return &wg_peer_get (peeri)->remote;
116 wg_index_set (noise_remote_t * remote)
118 wg_main_t *wmp = &wg_main;
119 u32 rnd_seed = (u32) (vlib_time_now (wmp->vlib_main) * 1e6);
121 wg_index_table_add (&wmp->index_table, remote->r_peer_idx, rnd_seed);
126 wg_index_drop (uint32_t key)
128 wg_main_t *wmp = &wg_main;
129 wg_index_table_del (&wmp->index_table, key);
132 static clib_error_t *
133 wg_if_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
135 vnet_hw_interface_t *hi;
139 hi = vnet_get_hw_interface (vnm, hw_if_index);
140 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ?
141 VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
142 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
144 wgii = wg_if_find_by_sw_if_index (hi->sw_if_index);
146 wg_if_peer_walk (wg_if_get (wgii), wg_peer_if_admin_state_change, NULL);
152 wg_if_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
154 /* The peers manage the adjacencies */
159 VNET_DEVICE_CLASS (wg_if_device_class) = {
160 .name = "Wireguard Tunnel",
161 .format_device_name = format_wg_if_name,
162 .admin_up_down_function = wg_if_admin_up_down,
165 VNET_HW_INTERFACE_CLASS(wg_hw_interface_class) = {
167 .update_adjacency = wg_if_update_adj,
168 .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA,
173 * Maintain a bitmap of allocated wg_if instance numbers.
175 #define WG_ITF_MAX_INSTANCE (16 * 1024)
178 wg_if_instance_alloc (u32 want)
181 * Check for dynamically allocated instance number.
187 bit = clib_bitmap_first_clear (wg_if_instances);
188 if (bit >= WG_ITF_MAX_INSTANCE)
192 wg_if_instances = clib_bitmap_set (wg_if_instances, bit, 1);
199 if (want >= WG_ITF_MAX_INSTANCE)
207 if (clib_bitmap_get (wg_if_instances, want))
213 * Grant allocation request.
215 wg_if_instances = clib_bitmap_set (wg_if_instances, want, 1);
221 wg_if_instance_free (u32 instance)
223 if (instance >= WG_ITF_MAX_INSTANCE)
228 if (clib_bitmap_get (wg_if_instances, instance) == 0)
233 wg_if_instances = clib_bitmap_set (wg_if_instances, instance, 0);
239 wg_if_create (u32 user_instance,
240 const u8 private_key[NOISE_PUBLIC_KEY_LEN],
241 u16 port, const ip_address_t * src_ip, u32 * sw_if_indexp)
243 vnet_main_t *vnm = vnet_get_main ();
244 u32 instance, hw_if_index;
245 vnet_hw_interface_t *hi;
247 noise_local_t *local;
249 ASSERT (sw_if_indexp);
251 *sw_if_indexp = (u32) ~ 0;
254 * Check if the required port is already in use
256 udp_dst_port_info_t *pi = udp_get_dst_port_info (&udp_main, port, UDP_IP4);
258 return VNET_API_ERROR_UDP_PORT_TAKEN;
261 * Allocate a wg_if instance. Either select on dynamically
262 * or try to use the desired user_instance number.
264 instance = wg_if_instance_alloc (user_instance);
266 return VNET_API_ERROR_INVALID_REGISTRATION;
269 struct noise_upcall upcall = {
270 .u_remote_get = wg_remote_get,
271 .u_index_set = wg_index_set,
272 .u_index_drop = wg_index_drop,
276 pool_get (noise_local_pool, local);
278 noise_local_init (local, &upcall);
279 if (!noise_local_set_private (local, private_key))
281 pool_put (noise_local_pool, local);
282 wg_if_instance_free (instance);
283 return VNET_API_ERROR_INVALID_REGISTRATION;
286 pool_get (wg_if_pool, wg_if);
288 /* tunnel index (or instance) */
289 u32 t_idx = wg_if - wg_if_pool;
291 wg_if->user_instance = instance;
292 if (~0 == wg_if->user_instance)
293 wg_if->user_instance = t_idx;
295 udp_register_dst_port (vlib_get_main (), port, wg_input_node.index, 1);
297 vec_validate_init_empty (wg_if_index_by_port, port, INDEX_INVALID);
298 wg_if_index_by_port[port] = wg_if - wg_if_pool;
301 wg_if->local_idx = local - noise_local_pool;
302 cookie_checker_update (&wg_if->cookie_checker, local->l_public);
304 hw_if_index = vnet_register_interface (vnm,
305 wg_if_device_class.index,
307 wg_hw_interface_class.index, t_idx);
309 hi = vnet_get_hw_interface (vnm, hw_if_index);
311 vec_validate_init_empty (wg_if_index_by_sw_if_index, hi->sw_if_index,
313 wg_if_index_by_sw_if_index[hi->sw_if_index] = t_idx;
315 ip_address_copy (&wg_if->src_ip, src_ip);
316 wg_if->sw_if_index = *sw_if_indexp = hi->sw_if_index;
322 wg_if_delete (u32 sw_if_index)
324 vnet_main_t *vnm = vnet_get_main ();
326 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
327 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
329 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
330 if (hw == 0 || hw->dev_class_index != wg_if_device_class.index)
331 return VNET_API_ERROR_INVALID_VALUE;
334 wg_if = wg_if_get (wg_if_find_by_sw_if_index (sw_if_index));
336 return VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
338 if (wg_if_instance_free (wg_if->user_instance) < 0)
339 return VNET_API_ERROR_INVALID_VALUE_2;
341 udp_unregister_dst_port (vlib_get_main (), wg_if->port, 1);
342 wg_if_index_by_port[wg_if->port] = INDEX_INVALID;
343 vnet_delete_hw_interface (vnm, hw->hw_if_index);
344 pool_put_index (noise_local_pool, wg_if->local_idx);
345 pool_put (wg_if_pool, wg_if);
351 wg_if_peer_add (wg_if_t * wgi, index_t peeri)
353 hash_set (wgi->peers, peeri, peeri);
355 if (1 == hash_elts (wgi->peers))
356 vnet_feature_enable_disable ("ip4-output", "wg-output-tun",
357 wgi->sw_if_index, 1, 0, 0);
361 wg_if_peer_remove (wg_if_t * wgi, index_t peeri)
363 hash_unset (wgi->peers, peeri);
365 if (0 == hash_elts (wgi->peers))
366 vnet_feature_enable_disable ("ip4-output", "wg-output-tun",
367 wgi->sw_if_index, 0, 0, 0);
371 wg_if_walk (wg_if_walk_cb_t fn, void *data)
376 pool_foreach_index (wgii, wg_if_pool)
378 if (WALK_STOP == fn(wgii, data))
385 wg_if_peer_walk (wg_if_t * wgi, wg_if_peer_walk_cb_t fn, void *data)
390 hash_foreach (peeri, val, wgi->peers,
392 if (WALK_STOP == fn(wgi, peeri, data))
397 return INDEX_INVALID;
402 wg_if_table_bind_v4 (ip4_main_t * im,
404 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
408 wg_if = wg_if_get (wg_if_find_by_sw_if_index (sw_if_index));
412 wg_peer_table_bind_ctx_t ctx = {
414 .old_fib_index = old_fib_index,
415 .new_fib_index = new_fib_index,
418 wg_if_peer_walk (wg_if, wg_peer_if_table_change, &ctx);
422 wg_if_table_bind_v6 (ip6_main_t * im,
424 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
428 wg_if = wg_if_get (wg_if_find_by_sw_if_index (sw_if_index));
432 wg_peer_table_bind_ctx_t ctx = {
434 .old_fib_index = old_fib_index,
435 .new_fib_index = new_fib_index,
438 wg_if_peer_walk (wg_if, wg_peer_if_table_change, &ctx);
441 static clib_error_t *
442 wg_if_module_init (vlib_main_t * vm)
445 ip4_table_bind_callback_t cb = {
446 .function = wg_if_table_bind_v4,
448 vec_add1 (ip4_main.table_bind_callbacks, cb);
451 ip6_table_bind_callback_t cb = {
452 .function = wg_if_table_bind_v6,
454 vec_add1 (ip6_main.table_bind_callbacks, cb);
461 VLIB_INIT_FUNCTION (wg_if_module_init) =
463 .runs_after = VLIB_INITS("ip_main_init"),
469 * fd.io coding-style-patch-verification: ON
472 * eval: (c-set-style "gnu")