wireguard: add dos mitigation support
[vpp.git] / src / plugins / wireguard / wireguard_peer.c
1 /*
2  * Copyright (c) 2020 Doc.ai and/or its affiliates.
3  * Copyright (c) 2020 Cisco 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/fib/fib_table.h>
19 #include <wireguard/wireguard_peer.h>
20 #include <wireguard/wireguard_if.h>
21 #include <wireguard/wireguard_messages.h>
22 #include <wireguard/wireguard_key.h>
23 #include <wireguard/wireguard_send.h>
24 #include <wireguard/wireguard.h>
25 #include <vnet/tunnel/tunnel_dp.h>
26
27 wg_peer_t *wg_peer_pool;
28
29 index_t *wg_peer_by_adj_index;
30
31 static void
32 wg_peer_endpoint_reset (wg_peer_endpoint_t * ep)
33 {
34   ip46_address_reset (&ep->addr);
35   ep->port = 0;
36 }
37
38 static void
39 wg_peer_endpoint_init (wg_peer_endpoint_t *ep, const ip46_address_t *addr,
40                        u16 port)
41 {
42   ip46_address_copy (&ep->addr, addr);
43   ep->port = port;
44 }
45
46 static void
47 wg_peer_clear (vlib_main_t * vm, wg_peer_t * peer)
48 {
49   index_t perri = peer - wg_peer_pool;
50   wg_timers_stop (peer);
51   wg_peer_update_flags (perri, WG_PEER_ESTABLISHED, false);
52   wg_peer_update_flags (perri, WG_PEER_STATUS_DEAD, true);
53   for (int i = 0; i < WG_N_TIMERS; i++)
54     {
55       peer->timers[i] = ~0;
56       peer->timers_dispatched[i] = 0;
57     }
58
59   peer->last_sent_handshake = vlib_time_now (vm) - (REKEY_TIMEOUT + 1);
60
61   clib_memset (&peer->cookie_maker, 0, sizeof (peer->cookie_maker));
62
63   wg_peer_endpoint_reset (&peer->src);
64   wg_peer_endpoint_reset (&peer->dst);
65
66   adj_index_t *adj_index;
67   vec_foreach (adj_index, peer->adj_indices)
68     {
69       if (INDEX_INVALID != *adj_index)
70         {
71           wg_peer_by_adj_index[*adj_index] = INDEX_INVALID;
72         }
73     }
74   peer->input_thread_index = ~0;
75   peer->output_thread_index = ~0;
76   peer->timer_wheel = 0;
77   peer->persistent_keepalive_interval = 0;
78   peer->timer_handshake_attempts = 0;
79   peer->last_sent_packet = 0;
80   peer->last_received_packet = 0;
81   peer->session_derived = 0;
82   peer->rehandshake_started = 0;
83   peer->new_handshake_interval_tick = 0;
84   peer->rehandshake_interval_tick = 0;
85   peer->timer_need_another_keepalive = false;
86   vec_free (peer->allowed_ips);
87   vec_free (peer->adj_indices);
88 }
89
90 static void
91 wg_peer_init (vlib_main_t * vm, wg_peer_t * peer)
92 {
93   peer->api_client_by_client_index = hash_create (0, sizeof (u32));
94   peer->api_clients = NULL;
95   wg_peer_clear (vm, peer);
96 }
97
98 static void
99 wg_peer_adj_stack (wg_peer_t *peer, adj_index_t ai)
100 {
101   ip_adjacency_t *adj;
102   u32 sw_if_index;
103   wg_if_t *wgi;
104   fib_protocol_t fib_proto;
105
106   if (!adj_is_valid (ai))
107     return;
108
109   adj = adj_get (ai);
110   sw_if_index = adj->rewrite_header.sw_if_index;
111   u8 is_ip4 = ip46_address_is_ip4 (&peer->src.addr);
112   fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
113
114   wgi = wg_if_get (wg_if_find_by_sw_if_index (sw_if_index));
115
116   if (!wgi)
117     return;
118
119   if (!vnet_sw_interface_is_admin_up (vnet_get_main (), wgi->sw_if_index))
120     {
121       adj_midchain_delegate_unstack (ai);
122     }
123   else
124     {
125       /* *INDENT-OFF* */
126       fib_prefix_t dst = {
127         .fp_len = is_ip4 ? 32 : 128,
128         .fp_proto = fib_proto,
129         .fp_addr = peer->dst.addr,
130       };
131       /* *INDENT-ON* */
132       u32 fib_index;
133
134       fib_index = fib_table_find (fib_proto, peer->table_id);
135
136       adj_midchain_delegate_stack (ai, fib_index, &dst);
137     }
138 }
139
140 static void
141 wg_peer_66_fixup (vlib_main_t *vm, const ip_adjacency_t *adj, vlib_buffer_t *b,
142                   const void *data)
143 {
144   u8 iph_offset = 0;
145   ip6_header_t *ip6_out;
146   ip6_header_t *ip6_in;
147
148   /* Must set locally originated otherwise we're not allowed to
149      fragment the packet later */
150   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
151
152   ip6_out = vlib_buffer_get_current (b);
153   iph_offset = vnet_buffer (b)->ip.save_rewrite_length;
154   ip6_in = vlib_buffer_get_current (b) + iph_offset;
155
156   ip6_out->ip_version_traffic_class_and_flow_label =
157     ip6_in->ip_version_traffic_class_and_flow_label;
158 }
159
160 static void
161 wg_peer_46_fixup (vlib_main_t *vm, const ip_adjacency_t *adj, vlib_buffer_t *b,
162                   const void *data)
163 {
164   u8 iph_offset = 0;
165   ip6_header_t *ip6_out;
166   ip4_header_t *ip4_in;
167
168   /* Must set locally originated otherwise we're not allowed to
169      fragment the packet later */
170   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
171
172   ip6_out = vlib_buffer_get_current (b);
173   iph_offset = vnet_buffer (b)->ip.save_rewrite_length;
174   ip4_in = vlib_buffer_get_current (b) + iph_offset;
175
176   u32 vtcfl = 0x6 << 28;
177   vtcfl |= ip4_in->tos << 20;
178   vtcfl |= vnet_buffer (b)->ip.flow_hash & 0x000fffff;
179
180   ip6_out->ip_version_traffic_class_and_flow_label =
181     clib_host_to_net_u32 (vtcfl);
182 }
183
184 static adj_midchain_fixup_t
185 wg_peer_get_fixup (wg_peer_t *peer, vnet_link_t lt)
186 {
187   if (!ip46_address_is_ip4 (&peer->dst.addr))
188     {
189       if (lt == VNET_LINK_IP4)
190         return (wg_peer_46_fixup);
191       if (lt == VNET_LINK_IP6)
192         return (wg_peer_66_fixup);
193     }
194   return (NULL);
195 }
196
197 walk_rc_t
198 wg_peer_if_admin_state_change (index_t peeri, void *data)
199 {
200   wg_peer_t *peer;
201   adj_index_t *adj_index;
202   peer = wg_peer_get (peeri);
203   vec_foreach (adj_index, peer->adj_indices)
204     {
205       wg_peer_adj_stack (peer, *adj_index);
206     }
207   return (WALK_CONTINUE);
208 }
209
210 walk_rc_t
211 wg_peer_if_adj_change (index_t peeri, void *data)
212 {
213   adj_index_t *adj_index = data;
214   adj_midchain_fixup_t fixup;
215   ip_adjacency_t *adj;
216   wg_peer_t *peer;
217   fib_prefix_t *allowed_ip;
218
219   adj = adj_get (*adj_index);
220
221   peer = wg_peer_get (peeri);
222   vec_foreach (allowed_ip, peer->allowed_ips)
223     {
224       if (fib_prefix_is_cover_addr_46 (allowed_ip,
225                                        &adj->sub_type.nbr.next_hop))
226         {
227           vec_add1 (peer->adj_indices, *adj_index);
228           vec_validate_init_empty (wg_peer_by_adj_index, *adj_index,
229                                    INDEX_INVALID);
230           wg_peer_by_adj_index[*adj_index] = peer - wg_peer_pool;
231
232           fixup = wg_peer_get_fixup (peer, adj_get_link_type (*adj_index));
233           adj_nbr_midchain_update_rewrite (*adj_index, fixup, NULL,
234                                            ADJ_FLAG_MIDCHAIN_IP_STACK,
235                                            vec_dup (peer->rewrite));
236
237           wg_peer_adj_stack (peer, *adj_index);
238           return (WALK_STOP);
239         }
240     }
241
242   return (WALK_CONTINUE);
243 }
244
245 adj_walk_rc_t
246 wg_peer_adj_walk (adj_index_t ai, void *data)
247 {
248   return wg_peer_if_adj_change ((*(index_t *) (data)), &ai) == WALK_CONTINUE ?
249            ADJ_WALK_RC_CONTINUE :
250            ADJ_WALK_RC_STOP;
251 }
252
253 walk_rc_t
254 wg_peer_if_delete (index_t peeri, void *data)
255 {
256   wg_peer_remove (peeri);
257   return (WALK_CONTINUE);
258 }
259
260 static int
261 wg_peer_fill (vlib_main_t *vm, wg_peer_t *peer, u32 table_id,
262               const ip46_address_t *dst, u16 port,
263               u16 persistent_keepalive_interval,
264               const fib_prefix_t *allowed_ips, u32 wg_sw_if_index)
265 {
266   index_t perri = peer - wg_peer_pool;
267   wg_peer_endpoint_init (&peer->dst, dst, port);
268
269   peer->table_id = table_id;
270   peer->wg_sw_if_index = wg_sw_if_index;
271   peer->timer_wheel = &wg_main.timer_wheel;
272   peer->persistent_keepalive_interval = persistent_keepalive_interval;
273   peer->last_sent_handshake = vlib_time_now (vm) - (REKEY_TIMEOUT + 1);
274   wg_peer_update_flags (perri, WG_PEER_STATUS_DEAD, false);
275
276   const wg_if_t *wgi = wg_if_get (wg_if_find_by_sw_if_index (wg_sw_if_index));
277
278   if (NULL == wgi)
279     return (VNET_API_ERROR_INVALID_INTERFACE);
280
281   ip_address_to_46 (&wgi->src_ip, &peer->src.addr);
282   peer->src.port = wgi->port;
283
284   u8 is_ip4 = ip46_address_is_ip4 (&peer->dst.addr);
285   peer->rewrite = wg_build_rewrite (&peer->src.addr, peer->src.port,
286                                     &peer->dst.addr, peer->dst.port, is_ip4);
287
288   u32 ii;
289   vec_validate (peer->allowed_ips, vec_len (allowed_ips) - 1);
290   vec_foreach_index (ii, allowed_ips)
291   {
292     peer->allowed_ips[ii] = allowed_ips[ii];
293   }
294
295   fib_protocol_t proto;
296   FOR_EACH_FIB_IP_PROTOCOL (proto)
297   {
298     adj_nbr_walk (wg_sw_if_index, proto, wg_peer_adj_walk, &perri);
299   }
300   return (0);
301 }
302
303 void
304 wg_peer_update_flags (index_t peeri, wg_peer_flags flag, bool add_del)
305 {
306   wg_peer_t *peer = wg_peer_get (peeri);
307   if ((add_del && (peer->flags & flag)) || (!add_del && !(peer->flags & flag)))
308     {
309       return;
310     }
311
312   peer->flags ^= flag;
313   wg_api_peer_event (peeri, peer->flags);
314 }
315
316 int
317 wg_peer_add (u32 tun_sw_if_index, const u8 public_key[NOISE_PUBLIC_KEY_LEN],
318              u32 table_id, const ip46_address_t *endpoint,
319              const fib_prefix_t *allowed_ips, u16 port,
320              u16 persistent_keepalive, u32 *peer_index)
321 {
322   wg_if_t *wg_if;
323   wg_peer_t *peer;
324   int rv;
325
326   vlib_main_t *vm = vlib_get_main ();
327
328   if (tun_sw_if_index == ~0)
329     return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
330
331   wg_if = wg_if_get (wg_if_find_by_sw_if_index (tun_sw_if_index));
332   if (!wg_if)
333     return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
334
335   /* *INDENT-OFF* */
336   pool_foreach (peer, wg_peer_pool)
337    {
338     if (!memcmp (peer->remote.r_public, public_key, NOISE_PUBLIC_KEY_LEN))
339     {
340       return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
341     }
342   }
343   /* *INDENT-ON* */
344
345   if (pool_elts (wg_peer_pool) > MAX_PEERS)
346     return (VNET_API_ERROR_LIMIT_EXCEEDED);
347
348   pool_get (wg_peer_pool, peer);
349
350   wg_peer_init (vm, peer);
351
352   rv = wg_peer_fill (vm, peer, table_id, endpoint, (u16) port,
353                      persistent_keepalive, allowed_ips, tun_sw_if_index);
354
355   if (rv)
356     {
357       wg_peer_clear (vm, peer);
358       pool_put (wg_peer_pool, peer);
359       return (rv);
360     }
361
362   noise_remote_init (&peer->remote, peer - wg_peer_pool, public_key,
363                      wg_if->local_idx);
364   cookie_maker_init (&peer->cookie_maker, public_key);
365
366   wg_send_handshake (vm, peer, false);
367   if (peer->persistent_keepalive_interval != 0)
368     {
369       wg_send_keepalive (vm, peer);
370     }
371
372   *peer_index = peer - wg_peer_pool;
373   wg_if_peer_add (wg_if, *peer_index);
374
375   return (0);
376 }
377
378 int
379 wg_peer_remove (index_t peeri)
380 {
381   wg_main_t *wmp = &wg_main;
382   wg_peer_t *peer = NULL;
383   wg_if_t *wgi;
384
385   if (pool_is_free_index (wg_peer_pool, peeri))
386     return VNET_API_ERROR_NO_SUCH_ENTRY;
387
388   peer = pool_elt_at_index (wg_peer_pool, peeri);
389
390   wgi = wg_if_get (wg_if_find_by_sw_if_index (peer->wg_sw_if_index));
391   wg_if_peer_remove (wgi, peeri);
392
393   noise_remote_clear (wmp->vlib_main, &peer->remote);
394   wg_peer_clear (wmp->vlib_main, peer);
395   pool_put (wg_peer_pool, peer);
396
397   return (0);
398 }
399
400 index_t
401 wg_peer_walk (wg_peer_walk_cb_t fn, void *data)
402 {
403   index_t peeri;
404
405   /* *INDENT-OFF* */
406   pool_foreach_index (peeri, wg_peer_pool)
407   {
408     if (WALK_STOP == fn(peeri, data))
409       return peeri;
410   }
411   /* *INDENT-ON* */
412   return INDEX_INVALID;
413 }
414
415 static u8 *
416 format_wg_peer_endpoint (u8 * s, va_list * args)
417 {
418   wg_peer_endpoint_t *ep = va_arg (*args, wg_peer_endpoint_t *);
419
420   s = format (s, "%U:%d", format_ip46_address, &ep->addr, IP46_TYPE_ANY,
421               ep->port);
422
423   return (s);
424 }
425
426 u8 *
427 format_wg_peer (u8 * s, va_list * va)
428 {
429   index_t peeri = va_arg (*va, index_t);
430   fib_prefix_t *allowed_ip;
431   adj_index_t *adj_index;
432   u8 key[NOISE_KEY_LEN_BASE64];
433   wg_peer_t *peer;
434
435   peer = wg_peer_get (peeri);
436   key_to_base64 (peer->remote.r_public, NOISE_PUBLIC_KEY_LEN, key);
437
438   s = format (
439     s,
440     "[%d] endpoint:[%U->%U] %U keep-alive:%d flags: %d, api-clients count: %d",
441     peeri, format_wg_peer_endpoint, &peer->src, format_wg_peer_endpoint,
442     &peer->dst, format_vnet_sw_if_index_name, vnet_get_main (),
443     peer->wg_sw_if_index, peer->persistent_keepalive_interval, peer->flags,
444     pool_elts (peer->api_clients));
445   s = format (s, "\n  adj:");
446   vec_foreach (adj_index, peer->adj_indices)
447     {
448       s = format (s, " %d", *adj_index);
449     }
450   s = format (s, "\n  key:%=s %U", key, format_hex_bytes,
451               peer->remote.r_public, NOISE_PUBLIC_KEY_LEN);
452   s = format (s, "\n  allowed-ips:");
453   vec_foreach (allowed_ip, peer->allowed_ips)
454   {
455     s = format (s, " %U", format_fib_prefix, allowed_ip);
456   }
457
458   return s;
459 }
460
461 /*
462  * fd.io coding-style-patch-verification: ON
463  *
464  * Local Variables:
465  * eval: (c-set-style "gnu")
466  * End:
467  */