5e2011b401ed2dcc13599d7609fde4e36166ba7e
[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
26 wg_peer_t *wg_peer_pool;
27
28 index_t *wg_peer_by_adj_index;
29
30 static void
31 wg_peer_endpoint_reset (wg_peer_endpoint_t * ep)
32 {
33   ip46_address_reset (&ep->addr);
34   ep->port = 0;
35 }
36
37 static void
38 wg_peer_endpoint_init (wg_peer_endpoint_t *ep, const ip46_address_t *addr,
39                        u16 port)
40 {
41   ip46_address_copy (&ep->addr, addr);
42   ep->port = port;
43 }
44
45 static void
46 wg_peer_clear (vlib_main_t * vm, wg_peer_t * peer)
47 {
48   wg_timers_stop (peer);
49   for (int i = 0; i < WG_N_TIMERS; i++)
50     {
51       peer->timers[i] = ~0;
52       peer->timers_dispatched[i] = 0;
53     }
54
55   peer->last_sent_handshake = vlib_time_now (vm) - (REKEY_TIMEOUT + 1);
56
57   clib_memset (&peer->cookie_maker, 0, sizeof (peer->cookie_maker));
58
59   wg_peer_endpoint_reset (&peer->src);
60   wg_peer_endpoint_reset (&peer->dst);
61
62   adj_index_t *adj_index;
63   vec_foreach (adj_index, peer->adj_indices)
64     {
65       if (INDEX_INVALID != *adj_index)
66         {
67           wg_peer_by_adj_index[*adj_index] = INDEX_INVALID;
68         }
69     }
70   peer->input_thread_index = ~0;
71   peer->output_thread_index = ~0;
72   peer->timer_wheel = 0;
73   peer->persistent_keepalive_interval = 0;
74   peer->timer_handshake_attempts = 0;
75   peer->last_sent_packet = 0;
76   peer->last_received_packet = 0;
77   peer->session_derived = 0;
78   peer->rehandshake_started = 0;
79   peer->new_handshake_interval_tick = 0;
80   peer->rehandshake_interval_tick = 0;
81   peer->timer_need_another_keepalive = false;
82   peer->is_dead = true;
83   vec_free (peer->allowed_ips);
84   vec_free (peer->adj_indices);
85 }
86
87 static void
88 wg_peer_init (vlib_main_t * vm, wg_peer_t * peer)
89 {
90   wg_peer_clear (vm, peer);
91 }
92
93 static u8 *
94 wg_peer_build_rewrite (const wg_peer_t * peer)
95 {
96   // v4 only for now
97   ip4_udp_header_t *hdr;
98   u8 *rewrite = NULL;
99
100   vec_validate (rewrite, sizeof (*hdr) - 1);
101   hdr = (ip4_udp_header_t *) rewrite;
102
103   hdr->ip4.ip_version_and_header_length = 0x45;
104   hdr->ip4.ttl = 64;
105   hdr->ip4.src_address = peer->src.addr.ip4;
106   hdr->ip4.dst_address = peer->dst.addr.ip4;
107   hdr->ip4.protocol = IP_PROTOCOL_UDP;
108   hdr->ip4.checksum = ip4_header_checksum (&hdr->ip4);
109
110   hdr->udp.src_port = clib_host_to_net_u16 (peer->src.port);
111   hdr->udp.dst_port = clib_host_to_net_u16 (peer->dst.port);
112   hdr->udp.checksum = 0;
113
114   return (rewrite);
115 }
116
117 static void
118 wg_peer_adj_stack (wg_peer_t *peer, adj_index_t ai)
119 {
120   ip_adjacency_t *adj;
121   u32 sw_if_index;
122   wg_if_t *wgi;
123
124   if (!adj_is_valid (ai))
125     return;
126
127   adj = adj_get (ai);
128   sw_if_index = adj->rewrite_header.sw_if_index;
129
130   wgi = wg_if_get (wg_if_find_by_sw_if_index (sw_if_index));
131
132   if (!wgi)
133     return;
134
135   if (!vnet_sw_interface_is_admin_up (vnet_get_main (), wgi->sw_if_index))
136     {
137       adj_midchain_delegate_unstack (ai);
138     }
139   else
140     {
141       /* *INDENT-OFF* */
142       fib_prefix_t dst = {
143         .fp_len = 32,
144         .fp_proto = FIB_PROTOCOL_IP4,
145         .fp_addr = peer->dst.addr,
146       };
147       /* *INDENT-ON* */
148       u32 fib_index;
149
150       fib_index = fib_table_find (FIB_PROTOCOL_IP4, peer->table_id);
151
152       adj_midchain_delegate_stack (ai, fib_index, &dst);
153     }
154 }
155
156 walk_rc_t
157 wg_peer_if_admin_state_change (index_t peeri, void *data)
158 {
159   wg_peer_t *peer;
160   adj_index_t *adj_index;
161   peer = wg_peer_get (peeri);
162   vec_foreach (adj_index, peer->adj_indices)
163     {
164       wg_peer_adj_stack (peer, *adj_index);
165     }
166   return (WALK_CONTINUE);
167 }
168
169 walk_rc_t
170 wg_peer_if_adj_change (index_t peeri, void *data)
171 {
172   adj_index_t *adj_index = data;
173   ip_adjacency_t *adj;
174   wg_peer_t *peer;
175   fib_prefix_t *allowed_ip;
176
177   adj = adj_get (*adj_index);
178
179   peer = wg_peer_get (peeri);
180   vec_foreach (allowed_ip, peer->allowed_ips)
181     {
182       if (fib_prefix_is_cover_addr_4 (allowed_ip,
183                                       &adj->sub_type.nbr.next_hop.ip4))
184         {
185           vec_add1 (peer->adj_indices, *adj_index);
186           vec_validate_init_empty (wg_peer_by_adj_index, *adj_index,
187                                    INDEX_INVALID);
188           wg_peer_by_adj_index[*adj_index] = peer - wg_peer_pool;
189
190           adj_nbr_midchain_update_rewrite (*adj_index, NULL, NULL,
191                                            ADJ_FLAG_MIDCHAIN_IP_STACK,
192                                            vec_dup (peer->rewrite));
193
194           wg_peer_adj_stack (peer, *adj_index);
195           return (WALK_STOP);
196         }
197     }
198
199   return (WALK_CONTINUE);
200 }
201
202 adj_walk_rc_t
203 wg_peer_adj_walk (adj_index_t ai, void *data)
204 {
205   return wg_peer_if_adj_change ((*(index_t *) (data)), &ai) == WALK_CONTINUE ?
206            ADJ_WALK_RC_CONTINUE :
207            ADJ_WALK_RC_STOP;
208 }
209
210 static int
211 wg_peer_fill (vlib_main_t *vm, wg_peer_t *peer, u32 table_id,
212               const ip46_address_t *dst, u16 port,
213               u16 persistent_keepalive_interval,
214               const fib_prefix_t *allowed_ips, u32 wg_sw_if_index)
215 {
216   wg_peer_endpoint_init (&peer->dst, dst, port);
217
218   peer->table_id = table_id;
219   peer->wg_sw_if_index = wg_sw_if_index;
220   peer->timer_wheel = &wg_main.timer_wheel;
221   peer->persistent_keepalive_interval = persistent_keepalive_interval;
222   peer->last_sent_handshake = vlib_time_now (vm) - (REKEY_TIMEOUT + 1);
223   peer->is_dead = false;
224
225   const wg_if_t *wgi = wg_if_get (wg_if_find_by_sw_if_index (wg_sw_if_index));
226
227   if (NULL == wgi)
228     return (VNET_API_ERROR_INVALID_INTERFACE);
229
230   ip_address_to_46 (&wgi->src_ip, &peer->src.addr);
231   peer->src.port = wgi->port;
232   peer->rewrite = wg_peer_build_rewrite (peer);
233
234   u32 ii;
235   vec_validate (peer->allowed_ips, vec_len (allowed_ips) - 1);
236   vec_foreach_index (ii, allowed_ips)
237   {
238     peer->allowed_ips[ii] = allowed_ips[ii];
239   }
240
241   index_t perri = peer - wg_peer_pool;
242   fib_protocol_t proto;
243   FOR_EACH_FIB_IP_PROTOCOL (proto)
244   {
245     adj_nbr_walk (wg_sw_if_index, proto, wg_peer_adj_walk, &perri);
246   }
247   return (0);
248 }
249
250 int
251 wg_peer_add (u32 tun_sw_if_index, const u8 public_key[NOISE_PUBLIC_KEY_LEN],
252              u32 table_id, const ip46_address_t *endpoint,
253              const fib_prefix_t *allowed_ips, u16 port,
254              u16 persistent_keepalive, u32 *peer_index)
255 {
256   wg_if_t *wg_if;
257   wg_peer_t *peer;
258   int rv;
259
260   vlib_main_t *vm = vlib_get_main ();
261
262   if (tun_sw_if_index == ~0)
263     return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
264
265   wg_if = wg_if_get (wg_if_find_by_sw_if_index (tun_sw_if_index));
266   if (!wg_if)
267     return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
268
269   /* *INDENT-OFF* */
270   pool_foreach (peer, wg_peer_pool)
271    {
272     if (!memcmp (peer->remote.r_public, public_key, NOISE_PUBLIC_KEY_LEN))
273     {
274       return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
275     }
276   }
277   /* *INDENT-ON* */
278
279   if (pool_elts (wg_peer_pool) > MAX_PEERS)
280     return (VNET_API_ERROR_LIMIT_EXCEEDED);
281
282   pool_get (wg_peer_pool, peer);
283
284   wg_peer_init (vm, peer);
285
286   rv = wg_peer_fill (vm, peer, table_id, endpoint, (u16) port,
287                      persistent_keepalive, allowed_ips, tun_sw_if_index);
288
289   if (rv)
290     {
291       wg_peer_clear (vm, peer);
292       pool_put (wg_peer_pool, peer);
293       return (rv);
294     }
295
296   noise_remote_init (&peer->remote, peer - wg_peer_pool, public_key,
297                      wg_if->local_idx);
298   cookie_maker_init (&peer->cookie_maker, public_key);
299
300   if (peer->persistent_keepalive_interval != 0)
301     {
302       wg_send_keepalive (vm, peer);
303     }
304
305   *peer_index = peer - wg_peer_pool;
306   wg_if_peer_add (wg_if, *peer_index);
307
308   return (0);
309 }
310
311 int
312 wg_peer_remove (index_t peeri)
313 {
314   wg_main_t *wmp = &wg_main;
315   wg_peer_t *peer = NULL;
316   wg_if_t *wgi;
317
318   if (pool_is_free_index (wg_peer_pool, peeri))
319     return VNET_API_ERROR_NO_SUCH_ENTRY;
320
321   peer = pool_elt_at_index (wg_peer_pool, peeri);
322
323   wgi = wg_if_get (wg_if_find_by_sw_if_index (peer->wg_sw_if_index));
324   wg_if_peer_remove (wgi, peeri);
325
326   noise_remote_clear (wmp->vlib_main, &peer->remote);
327   wg_peer_clear (wmp->vlib_main, peer);
328   pool_put (wg_peer_pool, peer);
329
330   return (0);
331 }
332
333 index_t
334 wg_peer_walk (wg_peer_walk_cb_t fn, void *data)
335 {
336   index_t peeri;
337
338   /* *INDENT-OFF* */
339   pool_foreach_index (peeri, wg_peer_pool)
340   {
341     if (WALK_STOP == fn(peeri, data))
342       return peeri;
343   }
344   /* *INDENT-ON* */
345   return INDEX_INVALID;
346 }
347
348 static u8 *
349 format_wg_peer_endpoint (u8 * s, va_list * args)
350 {
351   wg_peer_endpoint_t *ep = va_arg (*args, wg_peer_endpoint_t *);
352
353   s = format (s, "%U:%d", format_ip46_address, &ep->addr, IP46_TYPE_ANY,
354               ep->port);
355
356   return (s);
357 }
358
359 u8 *
360 format_wg_peer (u8 * s, va_list * va)
361 {
362   index_t peeri = va_arg (*va, index_t);
363   fib_prefix_t *allowed_ip;
364   adj_index_t *adj_index;
365   u8 key[NOISE_KEY_LEN_BASE64];
366   wg_peer_t *peer;
367
368   peer = wg_peer_get (peeri);
369   key_to_base64 (peer->remote.r_public, NOISE_PUBLIC_KEY_LEN, key);
370
371   s = format (s, "[%d] endpoint:[%U->%U] %U keep-alive:%d", peeri,
372               format_wg_peer_endpoint, &peer->src, format_wg_peer_endpoint,
373               &peer->dst, format_vnet_sw_if_index_name, vnet_get_main (),
374               peer->wg_sw_if_index, peer->persistent_keepalive_interval);
375   s = format (s, "\n  adj:");
376   vec_foreach (adj_index, peer->adj_indices)
377     {
378       s = format (s, " %d", adj_index);
379     }
380   s = format (s, "\n  key:%=s %U", key, format_hex_bytes,
381               peer->remote.r_public, NOISE_PUBLIC_KEY_LEN);
382   s = format (s, "\n  allowed-ips:");
383   vec_foreach (allowed_ip, peer->allowed_ips)
384   {
385     s = format (s, " %U", format_fib_prefix, allowed_ip);
386   }
387
388   return s;
389 }
390
391 /*
392  * fd.io coding-style-patch-verification: ON
393  *
394  * Local Variables:
395  * eval: (c-set-style "gnu")
396  * End:
397  */