summaryrefslogtreecommitdiffstats
path: root/src/plugins/wireguard/wireguard_api.c
blob: 5dd4f86f9105f5a2b808a075d68001869952310a (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
/*
 * Copyright (c) 2020 Cisco and/or its affiliates.
 * Copyright (c) 2020 Doc.ai 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/vnet.h>
#include <vlibmemory/api.h>

#include <vnet/format_fns.h>
#include <vnet/ip/ip_types_api.h>
#include <vlibapi/api.h>

#include <wireguard/wireguard.api_enum.h>
#include <wireguard/wireguard.api_types.h>

#include <wireguard/wireguard_key.h>
#include <wireguard/wireguard.h>
#include <wireguard/wireguard_if.h>
#include <wireguard/wireguard_peer.h>

#define REPLY_MSG_ID_BASE wmp->msg_id_base
#include <vlibapi/api_helper_macros.h>

static void
  vl_api_wireguard_interface_create_t_handler
  (vl_api_wireguard_interface_create_t * mp)
{
  vl_api_wireguard_interface_create_reply_t *rmp;
  wg_main_t *wmp = &wg_main;
  u8 private_key[NOISE_PUBLIC_KEY_LEN];
  ip_address_t src;
  u32 sw_if_index = ~0;
  int rv = 0;

  wg_feature_init (wmp);

  ip_address_decode2 (&mp->interface.src_ip, &src);

  if (mp->generate_key)
    curve25519_gen_secret (private_key);
  else
    clib_memcpy (private_key, mp->interface.private_key, NOISE_PUBLIC_KEY_LEN);

  rv = wg_if_create (ntohl (mp->interface.user_instance), private_key,
		     ntohs (mp->interface.port), &src, &sw_if_index);

  /* *INDENT-OFF* */
  REPLY_MACRO2(VL_API_WIREGUARD_INTERFACE_CREATE_REPLY,
  {
    rmp->sw_if_index = htonl(sw_if_index);
  });
  /* *INDENT-ON* */
}

static void
  vl_api_wireguard_interface_delete_t_handler
  (vl_api_wireguard_interface_delete_t * mp)
{
  vl_api_wireguard_interface_delete_reply_t *rmp;
  wg_main_t *wmp = &wg_main;
  int rv = 0;

  wg_feature_init (wmp);

  VALIDATE_SW_IF_INDEX (mp);

  rv = wg_if_delete (ntohl (mp->sw_if_index));

  BAD_SW_IF_INDEX_LABEL;

  /* *INDENT-OFF* */
  REPLY_MACRO(VL_API_WIREGUARD_INTERFACE_DELETE_REPLY);
  /* *INDENT-ON* */
}

typedef struct wg_deatils_walk_t_
{
  vl_api_registration_t *reg;
  u32 context;
  u8 show_private_key;
} wg_deatils_walk_t;

static walk_rc_t
wireguard_if_send_details (index_t wgii, void *data)
{
  vl_api_wireguard_interface_details_t *rmp;
  wg_deatils_walk_t *ctx = data;
  const wg_if_t *wgi;
  const noise_local_t *local;

  wgi = wg_if_get (wgii);
  local = noise_local_get (wgi->local_idx);

  rmp = vl_msg_api_alloc_zero (sizeof (*rmp));
  rmp->_vl_msg_id = htons (VL_API_WIREGUARD_INTERFACE_DETAILS +
			   wg_main.msg_id_base);

  if (ctx->show_private_key)
    clib_memcpy (rmp->interface.private_key,
		 local->l_private, NOISE_PUBLIC_KEY_LEN);
  clib_memcpy (rmp->interface.public_key,
	       local->l_public, NOISE_PUBLIC_KEY_LEN);
  rmp->interface.sw_if_index = htonl (wgi->sw_if_index);
  rmp->interface.port = htons (wgi->port);
  ip_address_encode2 (&wgi->src_ip, &rmp->interface.src_ip);

  rmp->context = ctx->context;

  vl_api_send_msg (ctx->reg, (u8 *) rmp);

  return (WALK_CONTINUE);
}

static void
vl_api_wireguard_interface_dump_t_handler (vl_api_wireguard_interface_dump_t *
					   mp)
{
  vl_api_registration_t *reg;
  wg_main_t *wmp = &wg_main;

  wg_feature_init (wmp);

  reg = vl_api_client_index_to_registration (mp->client_index);
  if (reg == 0)
    return;

  wg_deatils_walk_t ctx = {
    .reg = reg,
    .context = mp->context,
    .show_private_key = mp->show_private_key,
  };

  wg_if_walk (wireguard_if_send_details, &ctx);
}

static void
vl_api_wireguard_peer_add_t_handler (vl_api_wireguard_peer_add_t * mp)
{
  vl_api_wireguard_peer_add_reply_t *rmp;
  wg_main_t *wmp = &wg_main;
  index_t peeri = INDEX_INVALID;
  int ii, rv = 0;

  ip_address_t endpoint;
  fib_prefix_t *allowed_ips = NULL;

  VALIDATE_SW_IF_INDEX (&(mp->peer));

  if (0 == mp->peer.n_allowed_ips)
    {
      rv = VNET_API_ERROR_INVALID_VALUE;
      goto done;
    }

  wg_feature_init (wmp);

  vec_validate (allowed_ips, mp->peer.n_allowed_ips - 1);
  ip_address_decode2 (&mp->peer.endpoint, &endpoint);

  for (ii = 0; ii < mp->peer.n_allowed_ips; ii++)
    ip_prefix_decode (&mp->peer.allowed_ips[ii], &allowed_ips[ii]);

  rv = wg_peer_add (ntohl (mp->peer.sw_if_index), mp->peer.public_key,
		    ntohl (mp->peer.table_id), &ip_addr_46 (&endpoint),
		    allowed_ips, ntohs (mp->peer.port),
		    ntohs (mp->peer.persistent_keepalive), &peeri);

  vec_free (allowed_ips);
done:
  BAD_SW_IF_INDEX_LABEL;
  /* *INDENT-OFF* */
  REPLY_MACRO2(VL_API_WIREGUARD_PEER_ADD_REPLY,
  {
    rmp->peer_index = ntohl (peeri);
  });
  /* *INDENT-ON* */
}

static void
vl_api_wireguard_peer_remove_t_handler (vl_api_wireguard_peer_remove_t * mp)
{
  vl_api_wireguard_peer_remove_reply_t *rmp;
  wg_main_t *wmp = &wg_main;
  int rv = 0;

  wg_feature_init (wmp);

  rv = wg_peer_remove (ntohl (mp->peer_index));

  /* *INDENT-OFF* */
  REPLY_MACRO(VL_API_WIREGUARD_PEER_REMOVE_REPLY);
  /* *INDENT-ON* */
}

static walk_rc_t
send_wg_peers_details (index_t peeri, void *data)
{
  vl_api_wireguard_peers_details_t *rmp;
  wg_deatils_walk_t *ctx = data;
  const wg_peer_t *peer;
  u8 n_allowed_ips;
  size_t ss;

  peer = wg_peer_get (peeri);
  n_allowed_ips = vec_len (peer->allowed_ips);

  ss = (sizeof (*rmp) + (n_allowed_ips * sizeof (rmp->peer.allowed_ips[0])));

  rmp = vl_msg_api_alloc_zero (ss);

  rmp->_vl_msg_id = htons (VL_API_WIREGUARD_PEERS_DETAILS +
			   wg_main.msg_id_base);

  if (peer->is_dead)
    rmp->peer.flags = WIREGUARD_PEER_STATUS_DEAD;
  clib_memcpy (rmp->peer.public_key,
	       peer->remote.r_public, NOISE_PUBLIC_KEY_LEN);

  ip_address_encode (&peer->dst.addr, IP46_TYPE_ANY, &rmp->peer.endpoint);
  rmp->peer.port = htons (peer->dst.port);
  rmp->peer.n_allowed_ips = n_allowed_ips;
  rmp->peer.sw_if_index = htonl (peer->wg_sw_if_index);

  int ii;
  for (ii = 0; ii < n_allowed_ips; ii++)
    ip_prefix_encode (&peer->allowed_ips[ii], &rmp->peer.allowed_ips[ii]);

  rmp->context = ctx->context;

  vl_api_send_msg (ctx->reg, (u8 *) rmp);

  return (WALK_CONTINUE);
}

static void
vl_api_wireguard_peers_dump_t_handler (vl_api_wireguard_peers_dump_t * mp)
{
  vl_api_registration_t *reg;
  wg_main_t *wmp = &wg_main;

  wg_feature_init (wmp);

  reg = vl_api_client_index_to_registration (mp->client_index);
  if (reg == NULL)
    return;

  wg_deatils_walk_t ctx = {
    .reg = reg,
    .context = mp->context,
  };

  wg_peer_walk (send_wg_peers_details, &ctx);
}

/* set tup the API message handling tables */
#include <wireguard/wireguard.api.c>
static clib_error_t *
wg_api_hookup (vlib_main_t * vm)
{
  wg_main_t *wmp = &wg_main;
  wmp->msg_id_base = setup_message_id_table ();
  return 0;
}

VLIB_API_INIT_FUNCTION (wg_api_hookup);

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