2 * Copyright (c) 2020 Doc.ai and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/vnet.h>
17 #include <vlibmemory/api.h>
19 #include <vnet/format_fns.h>
20 #include <vnet/ip/ip_types_api.h>
21 #include <vlibapi/api.h>
23 #include <wireguard/wireguard.api_enum.h>
24 #include <wireguard/wireguard.api_types.h>
26 #include <wireguard/wireguard_key.h>
27 #include <wireguard/wireguard.h>
28 #include <wireguard/wireguard_if.h>
29 #include <wireguard/wireguard_peer.h>
31 #define REPLY_MSG_ID_BASE wmp->msg_id_base
32 #include <vlibapi/api_helper_macros.h>
35 vl_api_wireguard_interface_create_t_handler
36 (vl_api_wireguard_interface_create_t * mp)
38 vl_api_wireguard_interface_create_reply_t *rmp;
39 wg_main_t *wmp = &wg_main;
40 u8 private_key[NOISE_PUBLIC_KEY_LEN];
45 ip_address_decode2 (&mp->interface.src_ip, &src);
47 if (AF_IP6 == ip_addr_version (&src))
48 rv = VNET_API_ERROR_INVALID_PROTOCOL;
52 curve25519_gen_secret (private_key);
54 clib_memcpy (private_key, mp->interface.private_key,
55 NOISE_PUBLIC_KEY_LEN);
57 rv = wg_if_create (ntohl (mp->interface.user_instance), private_key,
58 ntohs (mp->interface.port), &src, &sw_if_index);
62 REPLY_MACRO2(VL_API_WIREGUARD_INTERFACE_CREATE_REPLY,
64 rmp->sw_if_index = htonl(sw_if_index);
70 vl_api_wireguard_interface_delete_t_handler
71 (vl_api_wireguard_interface_delete_t * mp)
73 vl_api_wireguard_interface_delete_reply_t *rmp;
74 wg_main_t *wmp = &wg_main;
77 VALIDATE_SW_IF_INDEX (mp);
79 rv = wg_if_delete (ntohl (mp->sw_if_index));
81 BAD_SW_IF_INDEX_LABEL;
84 REPLY_MACRO(VL_API_WIREGUARD_INTERFACE_DELETE_REPLY);
88 typedef struct wg_deatils_walk_t_
90 vl_api_registration_t *reg;
95 wireguard_if_send_details (index_t wgii, void *data)
97 vl_api_wireguard_interface_details_t *rmp;
98 wg_deatils_walk_t *ctx = data;
100 const noise_local_t *local;
102 wgi = wg_if_get (wgii);
103 local = noise_local_get (wgi->local_idx);
105 rmp = vl_msg_api_alloc_zero (sizeof (*rmp));
106 rmp->_vl_msg_id = htons (VL_API_WIREGUARD_INTERFACE_DETAILS +
107 wg_main.msg_id_base);
109 clib_memcpy (rmp->interface.private_key,
110 local->l_private, NOISE_PUBLIC_KEY_LEN);
111 rmp->interface.sw_if_index = htonl (wgi->sw_if_index);
112 rmp->interface.port = htons (wgi->port);
113 ip_address_encode2 (&wgi->src_ip, &rmp->interface.src_ip);
115 rmp->context = ctx->context;
117 vl_api_send_msg (ctx->reg, (u8 *) rmp);
119 return (WALK_CONTINUE);
123 vl_api_wireguard_interface_dump_t_handler (vl_api_wireguard_interface_dump_t *
126 vl_api_registration_t *reg;
128 reg = vl_api_client_index_to_registration (mp->client_index);
132 wg_deatils_walk_t ctx = {
134 .context = mp->context,
137 wg_if_walk (wireguard_if_send_details, &ctx);
141 vl_api_wireguard_peer_add_t_handler (vl_api_wireguard_peer_add_t * mp)
143 vl_api_wireguard_peer_add_reply_t *rmp;
144 wg_main_t *wmp = &wg_main;
145 index_t peeri = INDEX_INVALID;
148 ip_address_t endpoint;
149 fib_prefix_t *allowed_ips = NULL;
151 VALIDATE_SW_IF_INDEX (&(mp->peer));
153 if (0 == mp->peer.n_allowed_ips)
155 rv = VNET_API_ERROR_INVALID_VALUE;
159 vec_validate (allowed_ips, mp->peer.n_allowed_ips - 1);
160 ip_address_decode2 (&mp->peer.endpoint, &endpoint);
162 for (ii = 0; ii < mp->peer.n_allowed_ips; ii++)
163 ip_prefix_decode (&mp->peer.allowed_ips[ii], &allowed_ips[ii]);
165 if (AF_IP6 == ip_addr_version (&endpoint) ||
166 FIB_PROTOCOL_IP6 == allowed_ips[0].fp_proto)
167 /* ip6 currently not supported, but the API needs to support it
168 * else we'll need to change it later, and that's a PITA */
169 rv = VNET_API_ERROR_INVALID_PROTOCOL;
171 rv = wg_peer_add (ntohl (mp->peer.sw_if_index),
173 ntohl (mp->peer.table_id),
174 &ip_addr_46 (&endpoint),
176 ntohs (mp->peer.port),
177 ntohs (mp->peer.persistent_keepalive), &peeri);
179 vec_free (allowed_ips);
181 BAD_SW_IF_INDEX_LABEL;
183 REPLY_MACRO2(VL_API_WIREGUARD_PEER_ADD_REPLY,
185 rmp->peer_index = ntohl (peeri);
191 vl_api_wireguard_peer_remove_t_handler (vl_api_wireguard_peer_remove_t * mp)
193 vl_api_wireguard_peer_remove_reply_t *rmp;
194 wg_main_t *wmp = &wg_main;
197 rv = wg_peer_remove (ntohl (mp->peer_index));
200 REPLY_MACRO(VL_API_WIREGUARD_PEER_REMOVE_REPLY);
205 send_wg_peers_details (index_t peeri, void *data)
207 vl_api_wireguard_peers_details_t *rmp;
208 wg_deatils_walk_t *ctx = data;
209 const wg_peer_t *peer;
213 peer = wg_peer_get (peeri);
214 n_allowed_ips = vec_len (peer->allowed_ips);
216 ss = (sizeof (*rmp) + (n_allowed_ips * sizeof (rmp->peer.allowed_ips[0])));
218 rmp = vl_msg_api_alloc_zero (ss);
220 rmp->_vl_msg_id = htons (VL_API_WIREGUARD_PEERS_DETAILS +
221 wg_main.msg_id_base);
224 rmp->peer.flags = WIREGUARD_PEER_STATUS_DEAD;
225 clib_memcpy (rmp->peer.public_key,
226 peer->remote.r_public, NOISE_PUBLIC_KEY_LEN);
228 ip_address_encode (&peer->dst.addr, IP46_TYPE_ANY, &rmp->peer.endpoint);
229 rmp->peer.port = htons (peer->dst.port);
230 rmp->peer.n_allowed_ips = n_allowed_ips;
231 rmp->peer.sw_if_index = htonl (peer->wg_sw_if_index);
234 for (ii = 0; ii < n_allowed_ips; ii++)
235 ip_prefix_encode (&peer->allowed_ips[ii].prefix,
236 &rmp->peer.allowed_ips[ii]);
238 rmp->context = ctx->context;
240 vl_api_send_msg (ctx->reg, (u8 *) rmp);
242 return (WALK_CONTINUE);
246 vl_api_wireguard_peers_dump_t_handler (vl_api_wireguard_peers_dump_t * mp)
248 vl_api_registration_t *reg;
250 reg = vl_api_client_index_to_registration (mp->client_index);
254 wg_deatils_walk_t ctx = {
256 .context = mp->context,
259 wg_peer_walk (send_wg_peers_details, &ctx);
262 /* set tup the API message handling tables */
263 #include <wireguard/wireguard.api.c>
264 static clib_error_t *
265 wg_api_hookup (vlib_main_t * vm)
267 wg_main_t *wmp = &wg_main;
268 wmp->msg_id_base = setup_message_id_table ();
272 VLIB_API_INIT_FUNCTION (wg_api_hookup);
275 * fd.io coding-style-patch-verification: ON
278 * eval: (c-set-style "gnu")