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