wireguard: fix license headers
[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_index_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
53   s = format (s, "[%d] %U src:%U port:%d",
54               wgii,
55               format_vnet_sw_if_index_name, vnet_get_main (),
56               wgi->sw_if_index, format_ip_address, &wgi->src_ip, wgi->port);
57
58   key_to_base64 (local->l_private, NOISE_PUBLIC_KEY_LEN, key);
59
60   s = format (s, " private-key:%s", key);
61   s =
62     format (s, " %U", format_hex_bytes, local->l_private,
63             NOISE_PUBLIC_KEY_LEN);
64
65   key_to_base64 (local->l_public, NOISE_PUBLIC_KEY_LEN, key);
66
67   s = format (s, " public-key:%s", key);
68
69   s =
70     format (s, " %U", format_hex_bytes, local->l_public,
71             NOISE_PUBLIC_KEY_LEN);
72
73   s = format (s, " mac-key: %U", format_hex_bytes,
74               &wgi->cookie_checker.cc_mac1_key, NOISE_PUBLIC_KEY_LEN);
75
76   return (s);
77 }
78
79 index_t
80 wg_if_find_by_sw_if_index (u32 sw_if_index)
81 {
82   if (vec_len (wg_if_index_by_sw_if_index) <= sw_if_index)
83     return INDEX_INVALID;
84   u32 ti = wg_if_index_by_sw_if_index[sw_if_index];
85   if (ti == ~0)
86     return INDEX_INVALID;
87
88   return (ti);
89 }
90
91 static walk_rc_t
92 wg_if_find_peer_by_public_key (index_t peeri, void *data)
93 {
94   uint8_t *public = data;
95   wg_peer_t *peer = wg_peer_get (peeri);
96
97   if (!memcmp (peer->remote.r_public, public, NOISE_PUBLIC_KEY_LEN))
98     return (WALK_STOP);
99   return (WALK_CONTINUE);
100 }
101
102 static noise_remote_t *
103 wg_remote_get (const uint8_t public[NOISE_PUBLIC_KEY_LEN])
104 {
105   index_t peeri;
106
107   peeri = wg_peer_walk (wg_if_find_peer_by_public_key, (void *) public);
108
109   if (INDEX_INVALID != peeri)
110     return &wg_peer_get (peeri)->remote;
111
112   return NULL;
113 }
114
115 static uint32_t
116 wg_index_set (noise_remote_t * remote)
117 {
118   wg_main_t *wmp = &wg_main;
119   u32 rnd_seed = (u32) (vlib_time_now (wmp->vlib_main) * 1e6);
120   u32 ret =
121     wg_index_table_add (&wmp->index_table, remote->r_peer_idx, rnd_seed);
122   return ret;
123 }
124
125 static void
126 wg_index_drop (uint32_t key)
127 {
128   wg_main_t *wmp = &wg_main;
129   wg_index_table_del (&wmp->index_table, key);
130 }
131
132 static clib_error_t *
133 wg_if_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
134 {
135   vnet_hw_interface_t *hi;
136   index_t wgii;
137   u32 hw_flags;
138
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);
143
144   wgii = wg_if_find_by_sw_if_index (hi->sw_if_index);
145
146   wg_if_peer_walk (wg_if_get (wgii), wg_peer_if_admin_state_change, NULL);
147
148   return (NULL);
149 }
150
151 void
152 wg_if_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
153 {
154   /* The peers manage the adjacencies */
155 }
156
157
158 /* *INDENT-OFF* */
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,
163 };
164
165 VNET_HW_INTERFACE_CLASS(wg_hw_interface_class) = {
166   .name = "Wireguard",
167   .update_adjacency = wg_if_update_adj,
168   .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA,
169 };
170 /* *INDENT-ON* */
171
172 /*
173  * Maintain a bitmap of allocated wg_if instance numbers.
174  */
175 #define WG_ITF_MAX_INSTANCE             (16 * 1024)
176
177 static u32
178 wg_if_instance_alloc (u32 want)
179 {
180   /*
181    * Check for dynamically allocated instance number.
182    */
183   if (~0 == want)
184     {
185       u32 bit;
186
187       bit = clib_bitmap_first_clear (wg_if_instances);
188       if (bit >= WG_ITF_MAX_INSTANCE)
189         {
190           return ~0;
191         }
192       wg_if_instances = clib_bitmap_set (wg_if_instances, bit, 1);
193       return bit;
194     }
195
196   /*
197    * In range?
198    */
199   if (want >= WG_ITF_MAX_INSTANCE)
200     {
201       return ~0;
202     }
203
204   /*
205    * Already in use?
206    */
207   if (clib_bitmap_get (wg_if_instances, want))
208     {
209       return ~0;
210     }
211
212   /*
213    * Grant allocation request.
214    */
215   wg_if_instances = clib_bitmap_set (wg_if_instances, want, 1);
216
217   return want;
218 }
219
220 static int
221 wg_if_instance_free (u32 instance)
222 {
223   if (instance >= WG_ITF_MAX_INSTANCE)
224     {
225       return -1;
226     }
227
228   if (clib_bitmap_get (wg_if_instances, instance) == 0)
229     {
230       return -1;
231     }
232
233   wg_if_instances = clib_bitmap_set (wg_if_instances, instance, 0);
234   return 0;
235 }
236
237
238 int
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)
242 {
243   vnet_main_t *vnm = vnet_get_main ();
244   u32 instance, hw_if_index;
245   vnet_hw_interface_t *hi;
246   wg_if_t *wg_if;
247   noise_local_t *local;
248
249   ASSERT (sw_if_indexp);
250
251   *sw_if_indexp = (u32) ~ 0;
252
253   /*
254    * Allocate a wg_if instance. Either select on dynamically
255    * or try to use the desired user_instance number.
256    */
257   instance = wg_if_instance_alloc (user_instance);
258   if (instance == ~0)
259     return VNET_API_ERROR_INVALID_REGISTRATION;
260
261   /* *INDENT-OFF* */
262   struct noise_upcall upcall =  {
263     .u_remote_get = wg_remote_get,
264     .u_index_set = wg_index_set,
265     .u_index_drop = wg_index_drop,
266   };
267   /* *INDENT-ON* */
268
269   pool_get (noise_local_pool, local);
270
271   noise_local_init (local, &upcall);
272   if (!noise_local_set_private (local, private_key))
273     {
274       pool_put (noise_local_pool, local);
275       wg_if_instance_free (instance);
276       return VNET_API_ERROR_INVALID_REGISTRATION;
277     }
278
279   pool_get (wg_if_pool, wg_if);
280
281   /* tunnel index (or instance) */
282   u32 t_idx = wg_if - wg_if_pool;
283
284   wg_if->user_instance = instance;
285   if (~0 == wg_if->user_instance)
286     wg_if->user_instance = t_idx;
287
288   udp_register_dst_port (vlib_get_main (), port, wg_input_node.index, 1);
289
290   vec_validate_init_empty (wg_if_index_by_port, port, INDEX_INVALID);
291   wg_if_index_by_port[port] = wg_if - wg_if_pool;
292
293   wg_if->port = port;
294   wg_if->local_idx = local - noise_local_pool;
295   cookie_checker_update (&wg_if->cookie_checker, local->l_public);
296
297   hw_if_index = vnet_register_interface (vnm,
298                                          wg_if_device_class.index,
299                                          t_idx,
300                                          wg_hw_interface_class.index, t_idx);
301
302   hi = vnet_get_hw_interface (vnm, hw_if_index);
303
304   vec_validate_init_empty (wg_if_index_by_sw_if_index, hi->sw_if_index,
305                            INDEX_INVALID);
306   wg_if_index_by_sw_if_index[hi->sw_if_index] = t_idx;
307
308   ip_address_copy (&wg_if->src_ip, src_ip);
309   wg_if->sw_if_index = *sw_if_indexp = hi->sw_if_index;
310
311   return 0;
312 }
313
314 int
315 wg_if_delete (u32 sw_if_index)
316 {
317   vnet_main_t *vnm = vnet_get_main ();
318
319   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
320     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
321
322   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
323   if (hw == 0 || hw->dev_class_index != wg_if_device_class.index)
324     return VNET_API_ERROR_INVALID_VALUE;
325
326   wg_if_t *wg_if;
327   wg_if = wg_if_get (wg_if_find_by_sw_if_index (sw_if_index));
328   if (NULL == wg_if)
329     return VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
330
331   if (wg_if_instance_free (wg_if->user_instance) < 0)
332     return VNET_API_ERROR_INVALID_VALUE_2;
333
334   udp_unregister_dst_port (vlib_get_main (), wg_if->port, 1);
335   wg_if_index_by_port[wg_if->port] = INDEX_INVALID;
336   vnet_delete_hw_interface (vnm, hw->hw_if_index);
337   pool_put_index (noise_local_pool, wg_if->local_idx);
338   pool_put (wg_if_pool, wg_if);
339
340   return 0;
341 }
342
343 void
344 wg_if_peer_add (wg_if_t * wgi, index_t peeri)
345 {
346   hash_set (wgi->peers, peeri, peeri);
347
348   if (1 == hash_elts (wgi->peers))
349     vnet_feature_enable_disable ("ip4-output", "wg-output-tun",
350                                  wgi->sw_if_index, 1, 0, 0);
351 }
352
353 void
354 wg_if_peer_remove (wg_if_t * wgi, index_t peeri)
355 {
356   hash_unset (wgi->peers, peeri);
357
358   if (0 == hash_elts (wgi->peers))
359     vnet_feature_enable_disable ("ip4-output", "wg-output-tun",
360                                  wgi->sw_if_index, 0, 0, 0);
361 }
362
363 void
364 wg_if_walk (wg_if_walk_cb_t fn, void *data)
365 {
366   index_t wgii;
367
368   /* *INDENT-OFF* */
369   pool_foreach_index (wgii, wg_if_pool,
370   {
371     if (WALK_STOP == fn(wgii, data))
372       break;
373   });
374   /* *INDENT-ON* */
375 }
376
377 index_t
378 wg_if_peer_walk (wg_if_t * wgi, wg_if_peer_walk_cb_t fn, void *data)
379 {
380   index_t peeri, val;
381
382   /* *INDENT-OFF* */
383   hash_foreach (peeri, val, wgi->peers,
384   {
385     if (WALK_STOP == fn(wgi, peeri, data))
386       return peeri;
387   });
388   /* *INDENT-ON* */
389
390   return INDEX_INVALID;
391 }
392
393
394 static void
395 wg_if_table_bind_v4 (ip4_main_t * im,
396                      uword opaque,
397                      u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
398 {
399   wg_if_t *wg_if;
400
401   wg_if = wg_if_get (wg_if_find_by_sw_if_index (sw_if_index));
402   if (NULL == wg_if)
403     return;
404
405   wg_peer_table_bind_ctx_t ctx = {
406     .af = AF_IP4,
407     .old_fib_index = old_fib_index,
408     .new_fib_index = new_fib_index,
409   };
410
411   wg_if_peer_walk (wg_if, wg_peer_if_table_change, &ctx);
412 }
413
414 static void
415 wg_if_table_bind_v6 (ip6_main_t * im,
416                      uword opaque,
417                      u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
418 {
419   wg_if_t *wg_if;
420
421   wg_if = wg_if_get (wg_if_find_by_sw_if_index (sw_if_index));
422   if (NULL == wg_if)
423     return;
424
425   wg_peer_table_bind_ctx_t ctx = {
426     .af = AF_IP6,
427     .old_fib_index = old_fib_index,
428     .new_fib_index = new_fib_index,
429   };
430
431   wg_if_peer_walk (wg_if, wg_peer_if_table_change, &ctx);
432 }
433
434 static clib_error_t *
435 wg_if_module_init (vlib_main_t * vm)
436 {
437   {
438     ip4_table_bind_callback_t cb = {
439       .function = wg_if_table_bind_v4,
440     };
441     vec_add1 (ip4_main.table_bind_callbacks, cb);
442   }
443   {
444     ip6_table_bind_callback_t cb = {
445       .function = wg_if_table_bind_v6,
446     };
447     vec_add1 (ip6_main.table_bind_callbacks, cb);
448   }
449
450   return (NULL);
451 }
452
453 /* *INDENT-OFF* */
454 VLIB_INIT_FUNCTION (wg_if_module_init) =
455 {
456   .runs_after = VLIB_INITS("ip_main_init"),
457 };
458 /* *INDENT-ON* */
459
460
461 /*
462  * fd.io coding-style-patch-verification: ON
463  *
464  * Local Variables:
465  * eval: (c-set-style "gnu")
466  * End:
467  */