wireguard: add events for peer
[vpp.git] / src / plugins / wireguard / wireguard_if.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/adj/adj_midchain.h>
18 #include <vnet/udp/udp.h>
19
20 #include <wireguard/wireguard_messages.h>
21 #include <wireguard/wireguard_if.h>
22 #include <wireguard/wireguard.h>
23 #include <wireguard/wireguard_peer.h>
24
25 /* pool of interfaces */
26 wg_if_t *wg_if_pool;
27
28 /* bitmap of Allocated WG_ITF instances */
29 static uword *wg_if_instances;
30
31 /* vector of interfaces key'd on their sw_if_index */
32 static index_t *wg_if_index_by_sw_if_index;
33
34 /* vector of interfaces key'd on their UDP port (in network order) */
35 index_t **wg_if_indexes_by_port;
36
37 static u8 *
38 format_wg_if_name (u8 * s, va_list * args)
39 {
40   u32 dev_instance = va_arg (*args, u32);
41   return format (s, "wg%d", dev_instance);
42 }
43
44 u8 *
45 format_wg_if (u8 * s, va_list * args)
46 {
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];
51
52   s = format (s, "[%d] %U src:%U port:%d",
53               wgii,
54               format_vnet_sw_if_index_name, vnet_get_main (),
55               wgi->sw_if_index, format_ip_address, &wgi->src_ip, wgi->port);
56
57   key_to_base64 (local->l_private, NOISE_PUBLIC_KEY_LEN, key);
58
59   s = format (s, " private-key:%s", key);
60   s =
61     format (s, " %U", format_hex_bytes, local->l_private,
62             NOISE_PUBLIC_KEY_LEN);
63
64   key_to_base64 (local->l_public, NOISE_PUBLIC_KEY_LEN, key);
65
66   s = format (s, " public-key:%s", key);
67
68   s =
69     format (s, " %U", format_hex_bytes, local->l_public,
70             NOISE_PUBLIC_KEY_LEN);
71
72   s = format (s, " mac-key: %U", format_hex_bytes,
73               &wgi->cookie_checker.cc_mac1_key, NOISE_PUBLIC_KEY_LEN);
74
75   return (s);
76 }
77
78 index_t
79 wg_if_find_by_sw_if_index (u32 sw_if_index)
80 {
81   if (vec_len (wg_if_index_by_sw_if_index) <= sw_if_index)
82     return INDEX_INVALID;
83   u32 ti = wg_if_index_by_sw_if_index[sw_if_index];
84   if (ti == ~0)
85     return INDEX_INVALID;
86
87   return (ti);
88 }
89
90 static walk_rc_t
91 wg_if_find_peer_by_public_key (index_t peeri, void *data)
92 {
93   uint8_t *public = data;
94   wg_peer_t *peer = wg_peer_get (peeri);
95
96   if (!memcmp (peer->remote.r_public, public, NOISE_PUBLIC_KEY_LEN))
97     return (WALK_STOP);
98   return (WALK_CONTINUE);
99 }
100
101 static noise_remote_t *
102 wg_remote_get (const uint8_t public[NOISE_PUBLIC_KEY_LEN])
103 {
104   index_t peeri;
105
106   peeri = wg_peer_walk (wg_if_find_peer_by_public_key, (void *) public);
107
108   if (INDEX_INVALID != peeri)
109     return &wg_peer_get (peeri)->remote;
110
111   return NULL;
112 }
113
114 static uint32_t
115 wg_index_set (noise_remote_t * remote)
116 {
117   wg_main_t *wmp = &wg_main;
118   u32 rnd_seed = (u32) (vlib_time_now (wmp->vlib_main) * 1e6);
119   u32 ret =
120     wg_index_table_add (&wmp->index_table, remote->r_peer_idx, rnd_seed);
121   return ret;
122 }
123
124 static void
125 wg_index_drop (uint32_t key)
126 {
127   wg_main_t *wmp = &wg_main;
128   wg_index_table_del (&wmp->index_table, key);
129 }
130
131 static clib_error_t *
132 wg_if_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
133 {
134   vnet_hw_interface_t *hi;
135   index_t wgii;
136   u32 hw_flags;
137
138   hi = vnet_get_hw_interface (vnm, hw_if_index);
139   hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ?
140               VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
141   vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
142
143   wgii = wg_if_find_by_sw_if_index (hi->sw_if_index);
144
145   wg_if_peer_walk (wg_if_get (wgii), wg_peer_if_admin_state_change, NULL);
146
147   return (NULL);
148 }
149
150 void
151 wg_if_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
152 {
153   index_t wgii;
154
155   wgii = wg_if_find_by_sw_if_index (sw_if_index);
156   wg_if_peer_walk (wg_if_get (wgii), wg_peer_if_adj_change, &ai);
157 }
158
159
160 /* *INDENT-OFF* */
161 VNET_DEVICE_CLASS (wg_if_device_class) = {
162   .name = "Wireguard Tunnel",
163   .format_device_name = format_wg_if_name,
164   .admin_up_down_function = wg_if_admin_up_down,
165 };
166
167 VNET_HW_INTERFACE_CLASS(wg_hw_interface_class) = {
168   .name = "Wireguard",
169   .update_adjacency = wg_if_update_adj,
170   .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA,
171 };
172 /* *INDENT-ON* */
173
174 /*
175  * Maintain a bitmap of allocated wg_if instance numbers.
176  */
177 #define WG_ITF_MAX_INSTANCE             (16 * 1024)
178
179 static u32
180 wg_if_instance_alloc (u32 want)
181 {
182   /*
183    * Check for dynamically allocated instance number.
184    */
185   if (~0 == want)
186     {
187       u32 bit;
188
189       bit = clib_bitmap_first_clear (wg_if_instances);
190       if (bit >= WG_ITF_MAX_INSTANCE)
191         {
192           return ~0;
193         }
194       wg_if_instances = clib_bitmap_set (wg_if_instances, bit, 1);
195       return bit;
196     }
197
198   /*
199    * In range?
200    */
201   if (want >= WG_ITF_MAX_INSTANCE)
202     {
203       return ~0;
204     }
205
206   /*
207    * Already in use?
208    */
209   if (clib_bitmap_get (wg_if_instances, want))
210     {
211       return ~0;
212     }
213
214   /*
215    * Grant allocation request.
216    */
217   wg_if_instances = clib_bitmap_set (wg_if_instances, want, 1);
218
219   return want;
220 }
221
222 static int
223 wg_if_instance_free (u32 instance)
224 {
225   if (instance >= WG_ITF_MAX_INSTANCE)
226     {
227       return -1;
228     }
229
230   if (clib_bitmap_get (wg_if_instances, instance) == 0)
231     {
232       return -1;
233     }
234
235   wg_if_instances = clib_bitmap_set (wg_if_instances, instance, 0);
236   return 0;
237 }
238
239
240 int
241 wg_if_create (u32 user_instance,
242               const u8 private_key[NOISE_PUBLIC_KEY_LEN],
243               u16 port, const ip_address_t * src_ip, u32 * sw_if_indexp)
244 {
245   vnet_main_t *vnm = vnet_get_main ();
246   u32 instance, hw_if_index;
247   vnet_hw_interface_t *hi;
248   wg_if_t *wg_if;
249   noise_local_t *local;
250
251   ASSERT (sw_if_indexp);
252
253   *sw_if_indexp = (u32) ~ 0;
254
255   /*
256    * Allocate a wg_if instance. Either select on dynamically
257    * or try to use the desired user_instance number.
258    */
259   instance = wg_if_instance_alloc (user_instance);
260   if (instance == ~0)
261     return VNET_API_ERROR_INVALID_REGISTRATION;
262
263   /* *INDENT-OFF* */
264   struct noise_upcall upcall =  {
265     .u_remote_get = wg_remote_get,
266     .u_index_set = wg_index_set,
267     .u_index_drop = wg_index_drop,
268   };
269   /* *INDENT-ON* */
270
271   pool_get (noise_local_pool, local);
272
273   noise_local_init (local, &upcall);
274   if (!noise_local_set_private (local, private_key))
275     {
276       pool_put (noise_local_pool, local);
277       wg_if_instance_free (instance);
278       return VNET_API_ERROR_INVALID_REGISTRATION;
279     }
280
281   pool_get (wg_if_pool, wg_if);
282
283   /* tunnel index (or instance) */
284   u32 t_idx = wg_if - wg_if_pool;
285
286   wg_if->user_instance = instance;
287   if (~0 == wg_if->user_instance)
288     wg_if->user_instance = t_idx;
289
290   vec_validate_init_empty (wg_if_indexes_by_port, port, NULL);
291   if (vec_len (wg_if_indexes_by_port[port]) == 0)
292     {
293       udp_register_dst_port (vlib_get_main (), port, wg4_input_node.index,
294                              UDP_IP4);
295       udp_register_dst_port (vlib_get_main (), port, wg6_input_node.index,
296                              UDP_IP6);
297     }
298
299   vec_add1 (wg_if_indexes_by_port[port], t_idx);
300
301   wg_if->port = port;
302   wg_if->local_idx = local - noise_local_pool;
303   cookie_checker_update (&wg_if->cookie_checker, local->l_public);
304
305   hw_if_index = vnet_register_interface (vnm,
306                                          wg_if_device_class.index,
307                                          t_idx,
308                                          wg_hw_interface_class.index, t_idx);
309
310   hi = vnet_get_hw_interface (vnm, hw_if_index);
311
312   vec_validate_init_empty (wg_if_index_by_sw_if_index, hi->sw_if_index,
313                            INDEX_INVALID);
314   wg_if_index_by_sw_if_index[hi->sw_if_index] = t_idx;
315
316   ip_address_copy (&wg_if->src_ip, src_ip);
317   wg_if->sw_if_index = *sw_if_indexp = hi->sw_if_index;
318
319   return 0;
320 }
321
322 int
323 wg_if_delete (u32 sw_if_index)
324 {
325   vnet_main_t *vnm = vnet_get_main ();
326
327   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
328     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
329
330   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
331   if (hw == 0 || hw->dev_class_index != wg_if_device_class.index)
332     return VNET_API_ERROR_INVALID_VALUE;
333
334   wg_if_t *wg_if;
335   index_t wgii = wg_if_find_by_sw_if_index (sw_if_index);
336   wg_if = wg_if_get (wgii);
337   if (NULL == wg_if)
338     return VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
339
340   if (wg_if_instance_free (wg_if->user_instance) < 0)
341     return VNET_API_ERROR_INVALID_VALUE_2;
342
343   // Remove peers before interface deletion
344   wg_if_peer_walk (wg_if, wg_peer_if_delete, NULL);
345
346   index_t *ii;
347   index_t *ifs = wg_if_indexes_get_by_port (wg_if->port);
348   vec_foreach (ii, ifs)
349     {
350       if (*ii == wgii)
351         {
352           vec_del1 (ifs, ifs - ii);
353           break;
354         }
355     }
356   if (vec_len (ifs) == 0)
357     {
358       udp_unregister_dst_port (vlib_get_main (), wg_if->port, 1);
359       udp_unregister_dst_port (vlib_get_main (), wg_if->port, 0);
360     }
361
362   vnet_delete_hw_interface (vnm, hw->hw_if_index);
363   pool_put_index (noise_local_pool, wg_if->local_idx);
364   pool_put (wg_if_pool, wg_if);
365
366   return 0;
367 }
368
369 void
370 wg_if_peer_add (wg_if_t * wgi, index_t peeri)
371 {
372   hash_set (wgi->peers, peeri, peeri);
373
374   if (1 == hash_elts (wgi->peers))
375     {
376       vnet_feature_enable_disable ("ip4-output", "wg4-output-tun",
377                                    wgi->sw_if_index, 1, 0, 0);
378       vnet_feature_enable_disable ("ip6-output", "wg6-output-tun",
379                                    wgi->sw_if_index, 1, 0, 0);
380     }
381 }
382
383 void
384 wg_if_peer_remove (wg_if_t * wgi, index_t peeri)
385 {
386   hash_unset (wgi->peers, peeri);
387
388   if (0 == hash_elts (wgi->peers))
389     {
390       vnet_feature_enable_disable ("ip4-output", "wg4-output-tun",
391                                    wgi->sw_if_index, 0, 0, 0);
392       vnet_feature_enable_disable ("ip6-output", "wg6-output-tun",
393                                    wgi->sw_if_index, 0, 0, 0);
394     }
395 }
396
397 void
398 wg_if_walk (wg_if_walk_cb_t fn, void *data)
399 {
400   index_t wgii;
401
402   /* *INDENT-OFF* */
403   pool_foreach_index (wgii, wg_if_pool)
404   {
405     if (WALK_STOP == fn(wgii, data))
406       break;
407   }
408   /* *INDENT-ON* */
409 }
410
411 index_t
412 wg_if_peer_walk (wg_if_t * wgi, wg_if_peer_walk_cb_t fn, void *data)
413 {
414   index_t peeri, val;
415
416   /* *INDENT-OFF* */
417   hash_foreach (peeri, val, wgi->peers, {
418     if (WALK_STOP == fn (peeri, data))
419       return peeri;
420   });
421   /* *INDENT-ON* */
422
423   return INDEX_INVALID;
424 }
425
426 /*
427  * fd.io coding-style-patch-verification: ON
428  *
429  * Local Variables:
430  * eval: (c-set-style "gnu")
431  * End:
432  */