summaryrefslogtreecommitdiffstats
path: root/src/plugins/wireguard/wireguard_peer.c
blob: 0c93a25f4d7e6ebe0a31c9d402fe69af27247820 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
 * Copyright (c) 2020 Doc.ai and/or its affiliates.
 * Copyright (c) 2020 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vnet/adj/adj_midchain.h>
#include <vnet/fib/fib_table.h>
#include <wireguard/wireguard_peer.h>
#include <wireguard/wireguard_if.h>
#include <wireguard/wireguard_messages.h>
#include <wireguard/wireguard_key.h>
#include <wireguard/wireguard_send.h>
#include <wireguard/wireguard.h>

static fib_source_t wg_fib_source;
wg_peer_t *wg_peer_pool;

index_t *wg_peer_by_adj_index;

static void
wg_peer_endpoint_reset (wg_peer_endpoint_t * ep)
{
  ip46_address_reset (&ep->addr);
  ep->port = 0;
}

static void
wg_peer_endpoint_init (wg_peer_endpoint_t * ep,
		       const ip46_address_t * addr, u16 port)
{
  ip46_address_copy (&ep->addr, addr);
  ep->port = port;
}

static void
wg_peer_fib_flush (wg_peer_t * peer)
{
  wg_peer_allowed_ip_t *allowed_ip;

  vec_foreach (allowed_ip, peer->allowed_ips)
  {
    fib_table_entry_delete_index (allowed_ip->fib_entry_index, wg_fib_source);
    allowed_ip->fib_entry_index = FIB_NODE_INDEX_INVALID;
  }
}

static void
wg_peer_fib_populate (wg_peer_t * peer, u32 fib_index)
{
  wg_peer_allowed_ip_t *allowed_ip;

  vec_foreach (allowed_ip, peer->allowed_ips)
  {
    allowed_ip->fib_entry_index =
      fib_table_entry_path_add (fib_index,
				&allowed_ip->prefix,
				wg_fib_source,
				FIB_ENTRY_FLAG_NONE,
				fib_proto_to_dpo (allowed_ip->
						  prefix.fp_proto),
				&peer->dst.addr, peer->wg_sw_if_index, ~0, 1,
				NULL, FIB_ROUTE_PATH_FLAG_NONE);
  }
}

static void
wg_peer_clear (vlib_main_t * vm, wg_peer_t * peer)
{
  wg_timers_stop (peer);
  for (int i = 0; i < WG_N_TIMERS; i++)
    {
      peer->timers[i] = ~0;
      peer->timers_dispatched[i] = 0;
    }

  peer->last_sent_handshake = vlib_time_now (vm) - (REKEY_TIMEOUT + 1);

  clib_memset (&peer->cookie_maker, 0, sizeof (peer->cookie_maker));

  wg_peer_endpoint_reset (&peer->src);
  wg_peer_endpoint_reset (&peer->dst);

  if (INDEX_INVALID != peer->adj_index)
    {
      adj_unlock (peer->adj_index);
      wg_peer_by_adj_index[peer->adj_index] = INDEX_INVALID;
    }
  wg_peer_fib_flush (peer);

  peer->input_thread_index = ~0;
  peer->output_thread_index = ~0;
  peer->adj_index = INDEX_INVALID;
  peer->timer_wheel = 0;
  peer->persistent_keepalive_interval = 0;
  peer->timer_handshake_attempts = 0;
  peer->last_sent_packet = 0;
  peer->last_received_packet = 0;
  peer->session_derived = 0;
  peer->rehandshake_started = 0;
  peer->new_handshake_interval_tick = 0;
  peer->rehandshake_interval_tick = 0;
  peer->timer_need_another_keepalive = false;
  peer->is_dead = true;
  vec_free (peer->allowed_ips);
}

static void
wg_peer_init (vlib_main_t * vm, wg_peer_t * peer)
{
  peer->adj_index = INDEX_INVALID;
  wg_peer_clear (vm, peer);
}

static u8 *
wg_peer_build_rewrite (const wg_peer_t * peer)
{
  // v4 only for now
  ip4_udp_header_t *hdr;
  u8 *rewrite = NULL;

  vec_validate (rewrite, sizeof (*hdr) - 1);
  hdr = (ip4_udp_header_t *) rewrite;

  hdr->ip4.ip_version_and_header_length = 0x45;
  hdr->ip4.ttl = 64;
  hdr->ip4.src_address = peer->src.addr.ip4;
  hdr->ip4.dst_address = peer->dst.addr.ip4;
  hdr->ip4.protocol = IP_PROTOCOL_UDP;
  hdr->ip4.checksum = ip4_header_checksum (&hdr->ip4);

  hdr->udp.src_port = clib_host_to_net_u16 (peer->src.port);
  hdr->udp.dst_port = clib_host_to_net_u16 (peer->dst.port);
  hdr->udp.checksum = 0;

  return (rewrite);
}

static void
wg_peer_adj_stack (wg_peer_t * peer)
{
  ip_adjacency_t *adj;
  u32 sw_if_index;
  wg_if_t *wgi;

  adj = adj_get (peer->adj_index);
  sw_if_index = adj->rewrite_header.sw_if_index;

  wgi = wg_if_get (wg_if_find_by_sw_if_index (sw_if_index));

  if (!wgi)
    return;

  if (!vnet_sw_interface_is_admin_up (vnet_get_main (), wgi->sw_if_index))
    {
      adj_midchain_delegate_unstack (peer->adj_index);
    }
  else
    {
      /* *INDENT-OFF* */
      fib_prefix_t dst = {
        .fp_len = 32,
        .fp_proto = FIB_PROTOCOL_IP4,
        .fp_addr = peer->dst.addr,
      };
      /* *INDENT-ON* */
      u32 fib_index;

      fib_index = fib_table_find (FIB_PROTOCOL_IP4, peer->table_id);

      adj_midchain_delegate_stack (peer->adj_index, fib_index, &dst);
    }
}

walk_rc_t
wg_peer_if_admin_state_change (wg_if_t * wgi, index_t peeri, void *data)
{
  wg_peer_adj_stack (wg_peer_get (peeri));

  return (WALK_CONTINUE);
}

walk_rc_t
wg_peer_if_table_change (wg_if_t * wgi, index_t peeri, void *data)
{
  wg_peer_table_bind_ctx_t *ctx = data;
  wg_peer_t *peer;

  peer = wg_peer_get (peeri);

  wg_peer_fib_flush (peer);
  wg_peer_fib_populate (peer, ctx->new_fib_index);

  return (WALK_CONTINUE);
}

static int
wg_peer_fill (vlib_main_t * vm, wg_peer_t * peer,
	      u32 table_id,
	      const ip46_address_t * dst,
	      u16 port,
	      u16 persistent_keepalive_interval,
	      const fib_prefix_t * allowed_ips, u32 wg_sw_if_index)
{
  wg_peer_endpoint_init (&peer->dst, dst, port);

  peer->table_id = table_id;
  peer->wg_sw_if_index = wg_sw_if_index;
  peer->timer_wheel = &wg_main.timer_wheel;
  peer->persistent_keepalive_interval = persistent_keepalive_interval;
  peer->last_sent_handshake = vlib_time_now (vm) - (REKEY_TIMEOUT + 1);
  peer->is_dead = false;

  const wg_if_t *wgi = wg_if_get (wg_if_find_by_sw_if_index (wg_sw_if_index));

  if (NULL == wgi)
    return (VNET_API_ERROR_INVALID_INTERFACE);

  ip_address_to_46 (&wgi->src_ip, &peer->src.addr);
  peer->src.port = wgi->port;

  /*
   * and an adjacency for the endpoint address in the overlay
   * on the wg interface
   */
  peer->rewrite = wg_peer_build_rewrite (peer);

  peer->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4,
					 VNET_LINK_IP4,
					 &peer->dst.addr, wgi->sw_if_index);

  vec_validate_init_empty (wg_peer_by_adj_index,
			   peer->adj_index, INDEX_INVALID);
  wg_peer_by_adj_index[peer->adj_index] = peer - wg_peer_pool;

  adj_nbr_midchain_update_rewrite (peer->adj_index,
				   NULL,
				   NULL,
				   ADJ_FLAG_MIDCHAIN_IP_STACK,
				   vec_dup (peer->rewrite));
  wg_peer_adj_stack (peer);

  /*
   * add a route in the overlay to each of the allowed-ips
   */
  u32 ii;

  vec_validate (peer->allowed_ips, vec_len (allowed_ips) - 1);

  vec_foreach_index (ii, allowed_ips)
  {
    peer->allowed_ips[ii].prefix = allowed_ips[ii];
  }

  wg_peer_fib_populate (peer,
			fib_table_get_index_for_sw_if_index
			(FIB_PROTOCOL_IP4, peer->wg_sw_if_index));

  return (0);
}

int
wg_peer_add (u32 tun_sw_if_index,
	     const u8 public_key[NOISE_PUBLIC_KEY_LEN],
	     u32 table_id,
	     const ip46_address_t * endpoint,
	     const fib_prefix_t * allowed_ips,
	     u16 port, u16 persistent_keepalive, u32 * peer_index)
{
  wg_if_t *wg_if;
  wg_peer_t *peer;
  int rv;

  vlib_main_t *vm = vlib_get_main ();

  if (tun_sw_if_index == ~0)
    return (VNET_API_ERROR_INVALID_SW_IF_INDEX);

  wg_if = wg_if_get (wg_if_find_by_sw_if_index (tun_sw_if_index));
  if (!wg_if)
    return (VNET_API_ERROR_INVALID_SW_IF_INDEX);

  /* *INDENT-OFF* */
  pool_foreach (peer, wg_peer_pool)
   {
    if (!memcmp (peer->remote.r_public, public_key, NOISE_PUBLIC_KEY_LEN))
    {
      return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
    }
  }
  /* *INDENT-ON* */

  if (pool_elts (wg_peer_pool) > MAX_PEERS)
    return (VNET_API_ERROR_LIMIT_EXCEEDED);

  pool_get (wg_peer_pool, peer);

  wg_peer_init (vm, peer);

  rv = wg_peer_fill (vm, peer, table_id, endpoint, (u16) port,
		     persistent_keepalive, allowed_ips, tun_sw_if_index);

  if (rv)
    {
      wg_peer_clear (vm, peer);
      pool_put (wg_peer_pool, peer);
      return (rv);
    }

  noise_remote_init (&peer->remote, peer - wg_peer_pool, public_key,
		     wg_if->local_idx);
  cookie_maker_init (&peer->cookie_maker, public_key);

  if (peer->persistent_keepalive_interval != 0)
    {
      wg_send_keepalive (vm, peer);
    }

  *peer_index = peer - wg_peer_pool;
  wg_if_peer_add (wg_if, *peer_index);

  return (0);
}

int
wg_peer_remove (index_t peeri)
{
  wg_main_t *wmp = &wg_main;
  wg_peer_t *peer = NULL;
  wg_if_t *wgi;

  if (pool_is_free_index (wg_peer_pool, peeri))
    return VNET_API_ERROR_NO_SUCH_ENTRY;

  peer = pool_elt_at_index (wg_peer_pool, peeri);

  wgi = wg_if_get (wg_if_find_by_sw_if_index (peer->wg_sw_if_index));
  wg_if_peer_remove (wgi, peeri);

  noise_remote_clear (wmp->vlib_main, &peer->remote);
  wg_peer_clear (wmp->vlib_main, peer);
  pool_put (wg_peer_pool, peer);

  return (0);
}

index_t
wg_peer_walk (wg_peer_walk_cb_t fn, void *data)
{
  index_t peeri;

  /* *INDENT-OFF* */
  pool_foreach_index (peeri, wg_peer_pool)
  {
    if (WALK_STOP == fn(peeri, data))
      return peeri;
  }
  /* *INDENT-ON* */
  return INDEX_INVALID;
}

static u8 *
format_wg_peer_endpoint (u8 * s, va_list * args)
{
  wg_peer_endpoint_t *ep = va_arg (*args, wg_peer_endpoint_t *);

  s = format (s, "%U:%d",
	      format_ip46_address, &ep->addr, IP46_TYPE_ANY, ep->port);

  return (s);
}

u8 *
format_wg_peer (u8 * s, va_list * va)
{
  index_t peeri = va_arg (*va, index_t);
  wg_peer_allowed_ip_t *allowed_ip;
  u8 key[NOISE_KEY_LEN_BASE64];
  wg_peer_t *peer;

  peer = wg_peer_get (peeri);
  key_to_base64 (peer->remote.r_public, NOISE_PUBLIC_KEY_LEN, key);

  s = format (s, "[%d] endpoint:[%U->%U] %U keep-alive:%d adj:%d",
	      peeri,
	      format_wg_peer_endpoint, &peer->src,
	      format_wg_peer_endpoint, &peer->dst,
	      format_vnet_sw_if_index_name, vnet_get_main (),
	      peer->wg_sw_if_index,
	      peer->persistent_keepalive_interval, peer->adj_index);
  s = format (s, "\n  key:%=s %U",
	      key, format_hex_bytes, peer->remote.r_public,
	      NOISE_PUBLIC_KEY_LEN);
  s = format (s, "\n  allowed-ips:");
  vec_foreach (allowed_ip, peer->allowed_ips)
  {
    s = format (s, " %U", format_fib_prefix, &allowed_ip->prefix);
  }

  return s;
}

static clib_error_t *
wg_peer_module_init (vlib_main_t * vm)
{
  /*
   * use a priority better than interface source, so that
   * if the same subnet is added to the wg interface and is
   * used as an allowed IP, then the wireguard soueced prefix
   * wins and traffic is routed to the endpoint rather than dropped
   */
  wg_fib_source = fib_source_allocate ("wireguard", 0x2, FIB_SOURCE_BH_API);

  return (NULL);
}

VLIB_INIT_FUNCTION (wg_peer_module_init);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */