wireguard: add async mode for encryption packets
[vpp.git] / src / plugins / wireguard / wireguard_api.c
1 /*
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <vnet/vnet.h>
18 #include <vlibmemory/api.h>
19
20 #include <vnet/format_fns.h>
21 #include <vnet/ip/ip_types_api.h>
22 #include <vlibapi/api.h>
23
24 #include <wireguard/wireguard.api_enum.h>
25 #include <wireguard/wireguard.api_types.h>
26
27 #include <wireguard/wireguard_key.h>
28 #include <wireguard/wireguard.h>
29 #include <wireguard/wireguard_if.h>
30
31 #define REPLY_MSG_ID_BASE wmp->msg_id_base
32 #include <wireguard/wireguard_peer.h>
33 #include <vlibapi/api_helper_macros.h>
34
35 static void
36   vl_api_wireguard_interface_create_t_handler
37   (vl_api_wireguard_interface_create_t * mp)
38 {
39   vl_api_wireguard_interface_create_reply_t *rmp;
40   wg_main_t *wmp = &wg_main;
41   u8 private_key[NOISE_PUBLIC_KEY_LEN];
42   ip_address_t src;
43   u32 sw_if_index = ~0;
44   int rv = 0;
45
46   wg_feature_init (wmp);
47
48   ip_address_decode2 (&mp->interface.src_ip, &src);
49
50   if (mp->generate_key)
51     curve25519_gen_secret (private_key);
52   else
53     clib_memcpy (private_key, mp->interface.private_key, NOISE_PUBLIC_KEY_LEN);
54
55   rv = wg_if_create (ntohl (mp->interface.user_instance), private_key,
56                      ntohs (mp->interface.port), &src, &sw_if_index);
57
58   REPLY_MACRO2(VL_API_WIREGUARD_INTERFACE_CREATE_REPLY,
59   {
60     rmp->sw_if_index = htonl(sw_if_index);
61   });
62 }
63
64 static void
65   vl_api_wireguard_interface_delete_t_handler
66   (vl_api_wireguard_interface_delete_t * mp)
67 {
68   vl_api_wireguard_interface_delete_reply_t *rmp;
69   wg_main_t *wmp = &wg_main;
70   int rv = 0;
71
72   wg_feature_init (wmp);
73
74   VALIDATE_SW_IF_INDEX (mp);
75
76   rv = wg_if_delete (ntohl (mp->sw_if_index));
77
78   BAD_SW_IF_INDEX_LABEL;
79
80   REPLY_MACRO(VL_API_WIREGUARD_INTERFACE_DELETE_REPLY);
81 }
82
83 typedef struct wg_deatils_walk_t_
84 {
85   vl_api_registration_t *reg;
86   u32 context;
87   u8 show_private_key;
88 } wg_deatils_walk_t;
89
90 static walk_rc_t
91 wireguard_if_send_details (index_t wgii, void *data)
92 {
93   vl_api_wireguard_interface_details_t *rmp;
94   wg_deatils_walk_t *ctx = data;
95   const wg_if_t *wgi;
96   const noise_local_t *local;
97
98   wgi = wg_if_get (wgii);
99   local = noise_local_get (wgi->local_idx);
100
101   rmp = vl_msg_api_alloc_zero (sizeof (*rmp));
102   rmp->_vl_msg_id = htons (VL_API_WIREGUARD_INTERFACE_DETAILS +
103                            wg_main.msg_id_base);
104
105   if (ctx->show_private_key)
106     clib_memcpy (rmp->interface.private_key,
107                  local->l_private, NOISE_PUBLIC_KEY_LEN);
108   clib_memcpy (rmp->interface.public_key,
109                local->l_public, NOISE_PUBLIC_KEY_LEN);
110   rmp->interface.sw_if_index = htonl (wgi->sw_if_index);
111   rmp->interface.port = htons (wgi->port);
112   ip_address_encode2 (&wgi->src_ip, &rmp->interface.src_ip);
113
114   rmp->context = ctx->context;
115
116   vl_api_send_msg (ctx->reg, (u8 *) rmp);
117
118   return (WALK_CONTINUE);
119 }
120
121 static void
122 vl_api_wireguard_interface_dump_t_handler (vl_api_wireguard_interface_dump_t *
123                                            mp)
124 {
125   vl_api_registration_t *reg;
126   wg_main_t *wmp = &wg_main;
127
128   wg_feature_init (wmp);
129
130   reg = vl_api_client_index_to_registration (mp->client_index);
131   if (reg == 0)
132     return;
133
134   wg_deatils_walk_t ctx = {
135     .reg = reg,
136     .context = mp->context,
137     .show_private_key = mp->show_private_key,
138   };
139
140   wg_if_walk (wireguard_if_send_details, &ctx);
141 }
142
143 static void
144 vl_api_wireguard_peer_add_t_handler (vl_api_wireguard_peer_add_t * mp)
145 {
146   vl_api_wireguard_peer_add_reply_t *rmp;
147   wg_main_t *wmp = &wg_main;
148   index_t peeri = INDEX_INVALID;
149   int ii, rv = 0;
150
151   ip_address_t endpoint;
152   fib_prefix_t *allowed_ips = NULL;
153
154   VALIDATE_SW_IF_INDEX (&(mp->peer));
155
156   if (0 == mp->peer.n_allowed_ips)
157     {
158       rv = VNET_API_ERROR_INVALID_VALUE;
159       goto done;
160     }
161
162   wg_feature_init (wmp);
163
164   vec_validate (allowed_ips, mp->peer.n_allowed_ips - 1);
165   ip_address_decode2 (&mp->peer.endpoint, &endpoint);
166
167   for (ii = 0; ii < mp->peer.n_allowed_ips; ii++)
168     ip_prefix_decode (&mp->peer.allowed_ips[ii], &allowed_ips[ii]);
169
170   rv = wg_peer_add (ntohl (mp->peer.sw_if_index), mp->peer.public_key,
171                     ntohl (mp->peer.table_id), &ip_addr_46 (&endpoint),
172                     allowed_ips, ntohs (mp->peer.port),
173                     ntohs (mp->peer.persistent_keepalive), &peeri);
174
175   vec_free (allowed_ips);
176 done:
177   BAD_SW_IF_INDEX_LABEL;
178
179   REPLY_MACRO2(VL_API_WIREGUARD_PEER_ADD_REPLY,
180   {
181     rmp->peer_index = ntohl (peeri);
182   });
183 }
184
185 static void
186 vl_api_wireguard_peer_remove_t_handler (vl_api_wireguard_peer_remove_t * mp)
187 {
188   vl_api_wireguard_peer_remove_reply_t *rmp;
189   wg_main_t *wmp = &wg_main;
190   int rv = 0;
191
192   wg_feature_init (wmp);
193
194   rv = wg_peer_remove (ntohl (mp->peer_index));
195
196   REPLY_MACRO(VL_API_WIREGUARD_PEER_REMOVE_REPLY);
197 }
198
199 static walk_rc_t
200 wg_api_send_peers_details (index_t peeri, void *data)
201 {
202   vl_api_wireguard_peers_details_t *rmp;
203   wg_deatils_walk_t *ctx = data;
204   const wg_peer_t *peer;
205   u8 n_allowed_ips;
206   size_t ss;
207
208   if (pool_is_free_index (wg_peer_pool, peeri))
209     return (WALK_CONTINUE);
210
211   peer = wg_peer_get (peeri);
212
213   n_allowed_ips = vec_len (peer->allowed_ips);
214
215   ss = (sizeof (*rmp) + (n_allowed_ips * sizeof (rmp->peer.allowed_ips[0])));
216
217   rmp = vl_msg_api_alloc_zero (ss);
218
219   rmp->_vl_msg_id = htons (VL_API_WIREGUARD_PEERS_DETAILS +
220                            wg_main.msg_id_base);
221
222   rmp->peer.flags = peer->flags;
223   clib_memcpy (rmp->peer.public_key,
224                peer->remote.r_public, NOISE_PUBLIC_KEY_LEN);
225
226   ip_address_encode (&peer->dst.addr, IP46_TYPE_ANY, &rmp->peer.endpoint);
227   rmp->peer.port = htons (peer->dst.port);
228   rmp->peer.n_allowed_ips = n_allowed_ips;
229   rmp->peer.sw_if_index = htonl (peer->wg_sw_if_index);
230
231   int ii;
232   for (ii = 0; ii < n_allowed_ips; ii++)
233     ip_prefix_encode (&peer->allowed_ips[ii], &rmp->peer.allowed_ips[ii]);
234
235   rmp->context = ctx->context;
236
237   vl_api_send_msg (ctx->reg, (u8 *) rmp);
238
239   return (WALK_CONTINUE);
240 }
241
242 static void
243 vl_api_wireguard_peers_dump_t_handler (vl_api_wireguard_peers_dump_t * mp)
244 {
245   vl_api_registration_t *reg;
246   wg_main_t *wmp = &wg_main;
247
248   wg_feature_init (wmp);
249
250   reg = vl_api_client_index_to_registration (mp->client_index);
251   if (reg == NULL)
252     return;
253
254   wg_deatils_walk_t ctx = {
255     .reg = reg,
256     .context = mp->context,
257   };
258
259   if (mp->peer_index == ~0)
260     wg_peer_walk (wg_api_send_peers_details, &ctx);
261   else
262     wg_api_send_peers_details (ntohl (mp->peer_index), &ctx);
263 }
264
265 static vpe_client_registration_t *
266 wg_api_client_lookup (wg_peer_t *peer, u32 client_index)
267 {
268   uword *p;
269   vpe_client_registration_t *api_client = NULL;
270
271   p = hash_get (peer->api_client_by_client_index, client_index);
272   if (p)
273     api_client = vec_elt_at_index (peer->api_clients, p[0]);
274
275   return api_client;
276 }
277
278 static walk_rc_t
279 wg_api_update_peer_api_client (index_t peeri, void *data)
280 {
281   if (pool_is_free_index (wg_peer_pool, peeri))
282     return (WALK_CONTINUE);
283
284   vl_api_want_wireguard_peer_events_t *mp = data;
285   wg_peer_t *peer = wg_peer_get (peeri);
286
287   if (ntohl (mp->sw_if_index) != ~0 &&
288       ntohl (mp->sw_if_index) != peer->wg_sw_if_index)
289     {
290       return (WALK_CONTINUE);
291     }
292
293   vpe_client_registration_t *api_client;
294
295   api_client = wg_api_client_lookup (peer, mp->client_index);
296
297   if (api_client)
298     {
299       if (mp->enable_disable)
300         {
301           return (WALK_CONTINUE);
302         }
303       hash_unset (peer->api_client_by_client_index, api_client->client_index);
304       pool_put (peer->api_clients, api_client);
305     }
306   if (mp->enable_disable)
307     {
308       pool_get (peer->api_clients, api_client);
309       clib_memset (api_client, 0, sizeof (vpe_client_registration_t));
310       api_client->client_index = mp->client_index;
311       api_client->client_pid = mp->pid;
312       hash_set (peer->api_client_by_client_index, mp->client_index,
313                 api_client - peer->api_clients);
314     }
315
316   return (WALK_CONTINUE);
317 }
318
319 static void
320 vl_api_want_wireguard_peer_events_t_handler (
321   vl_api_want_wireguard_peer_events_t *mp)
322 {
323   wg_main_t *wmp = &wg_main;
324   vl_api_want_wireguard_peer_events_reply_t *rmp;
325   int rv = 0;
326
327   wg_feature_init (wmp);
328
329   if (mp->peer_index == ~0)
330     wg_peer_walk (wg_api_update_peer_api_client, mp);
331   else
332     wg_api_update_peer_api_client (ntohl (mp->peer_index), mp);
333
334   REPLY_MACRO (VL_API_WANT_WIREGUARD_PEER_EVENTS_REPLY);
335 }
336
337 void
338 wg_api_send_peer_event (vl_api_registration_t *rp, index_t peer_index,
339                         wg_peer_flags flags)
340 {
341   vl_api_wireguard_peer_event_t *mp = vl_msg_api_alloc (sizeof (*mp));
342   clib_memset (mp, 0, sizeof (*mp));
343
344   mp->_vl_msg_id = htons (VL_API_WIREGUARD_PEER_EVENT + wg_main.msg_id_base);
345   mp->peer_index = htonl (peer_index);
346   mp->flags = flags;
347
348   vl_api_send_msg (rp, (u8 *) mp);
349 }
350
351 void
352 wg_api_peer_event (index_t peeri, wg_peer_flags flags)
353 {
354   wg_peer_t *peer = wg_peer_get (peeri);
355   vpe_client_registration_t *api_client;
356   vl_api_registration_t *rp;
357
358   pool_foreach (api_client, peer->api_clients)
359     {
360       rp = vl_api_client_index_to_registration (api_client->client_index);
361       if (rp)
362         {
363           wg_api_send_peer_event (rp, peeri, flags);
364         }
365     };
366 }
367
368 static void
369 vl_api_wg_set_async_mode_t_handler (vl_api_wg_set_async_mode_t *mp)
370 {
371   wg_main_t *wmp = &wg_main;
372   vl_api_wg_set_async_mode_reply_t *rmp;
373   int rv = 0;
374
375   wg_set_async_mode (mp->async_enable);
376
377   REPLY_MACRO (VL_API_WG_SET_ASYNC_MODE_REPLY);
378 }
379
380 /* set tup the API message handling tables */
381 #include <wireguard/wireguard.api.c>
382 static clib_error_t *
383 wg_api_hookup (vlib_main_t * vm)
384 {
385   wg_main_t *wmp = &wg_main;
386   wmp->msg_id_base = setup_message_id_table ();
387   return 0;
388 }
389
390 VLIB_API_INIT_FUNCTION (wg_api_hookup);
391
392 /*
393  * fd.io coding-style-patch-verification: ON
394  *
395  * Local Variables:
396  * eval: (c-set-style "gnu")
397  * End:
398  */