summaryrefslogtreecommitdiff
path: root/sysdep/unix/wg_user.c
blob: 6920d9ef74d4706c30df0397987be444f39bfc49 (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
#include <stdio.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
#include "lib/lists.h"
#include "lib/ip.h"
#include "lib/socket.h"
#include "nest/iface.h"
#include "sysdep/unix/unix.h"
#include "sysdep/unix/wg_user.h"
#include "sysdep/linux/wireguard.h"

static
socklen_t get_socket_path(const char *ifname, char *buf, uint size)
{
  int pos = 0;

  if (size < 1)
    return 0;

#ifdef ANDROID
  /* Abstract socket */
  buf[pos++] = 0;
#endif
  bsnprintf(buf+pos, size-pos, SOCKET_PATH "%s.sock", ifname);
  return pos + strlen(buf+pos);
}

bool wg_has_userspace(const char *ifname)
{
  struct stat sb;
  char tmp[sizeof(struct sockaddr_un)];
  socklen_t tmplen = get_socket_path(ifname, tmp, sizeof(tmp));

  if (tmplen > 0 && tmp[0] == 0)
    /* System with abstract socket (Android) always use WireGuard's userspace
       implementation. */
    return true;
  else if (stat(tmp, &sb) == 0)
    return (sb.st_mode & S_IFMT) == S_IFSOCK;
  else
    {
      DBG(L_TRACE "WG: no socket %s", tmp);
      return false;
    }
}

/* NULL=receiving turned off, returns 1 to clear rx buffer */
static
int user_rx_hook(struct birdsock *sk UNUSED, uint size UNUSED)
{
  DBG(L_TRACE "WG: RX %p %d", sk, size);
  rfree(sk);
  return 1;
}

static
void user_tx_hook(struct birdsock *bs)
{
  DBG(L_TRACE "WG: TX %p %d", bs, bs->tpos - bs->ttx);

  uint size = (uintptr_t)bs->data;

  if (size > 0)
    {
      int res = sk_send(bs, bs->tbsize - size);

      /* Send data, <0=err, >0=ok, 0=sleep */
      log(L_TRACE "WG: send %d", res);

      if (res != 0)
	{
	  //rfree(sock);
	  //shutdown(sock->fd, SHUT_WR);
	}

      bs->data = NULL;
    }
}

/* errno or zero if EOF */
static
void user_err_hook(struct birdsock *bs, int err)
{
  /* if (err == 0) */
  /*   return; */

  log(L_TRACE "WG: ERR %p %d %s", bs, err, bs->err);
  rfree(bs);
}

static void
wg_puts(const char *str, byte **buf, uint *size)
{
  int len = strlen(str);
  if (0 < len && len < (int)*size)
    {
      strcpy(*buf, str);
      *size -= len;
      *buf += len;
    }
  else
    *size = 0;
}

static void
wg_put_str(const char *key, const char *value, byte **buf, uint *size)
{
  char tmp[128];

  int len = snprintf(tmp, sizeof(tmp), "%s=%s\n", key, value);
  if (0 < len && len < (int)*size)
    {
      strcpy(*buf, tmp);
      *size -= len;
      *buf += len;
    }
  else
    *size = 0;
}

static void
wg_put_u16(const char *key, u16 value, byte **buf, uint *size)
{
  char tmp[64];

  int len = snprintf(tmp, sizeof(tmp), "%u", value);
  if (len > 0)
    wg_put_str(key, tmp, buf, size);
  else
    *size = 0;
}

static void
wg_put_bool(const char *key, bool value, byte **buf, uint *size)
{
  if (value)
    wg_put_str(key, "true", buf, size);
}

static void
wg_put_key(const char *key, wg_key value, byte **buf, uint *size)
{
  char tmp[128];

  for (uint i=0; i < sizeof(wg_key); i++)
    bsnprintf(tmp+2*i, sizeof(tmp)-2*i, "%02x", value[i]);

  wg_put_str(key, tmp, buf, size);
}

static void
wg_put_endpoint(const wg_endpoint *endpoint, byte **buf, uint *size)
{
  char tmp[INET6_ADDRSTRLEN + 16];
  ip_addr ip;
  struct iface *ifa = NULL;
  uint port = 0;

  if (sockaddr_read((sockaddr*)&endpoint->addr, endpoint->addr.sa_family,
		    &ip, &ifa, &port) == 0)
    {
      char *pos = NULL;

      if (ipa_is_ip4(ip))
	pos = ip4_ntop(ipa_to_ip4(ip), tmp);
      else
	{
	  tmp[0] = '[';
	  pos = ip6_ntop(ipa_to_ip6(ip), tmp + 1);
	  if (ifa)
	    pos += bsprintf(pos, "%%%u", ifa->index);
	  *pos++ = ']';
	}

      bsprintf(pos, ":%u", port);
      wg_put_str("endpoint", tmp, buf, size);
    }
}

static void
wg_put_allowedip(wg_allowedip *allowedip, byte **buf, uint *size)
{
  char tmp[INET6_ADDRSTRLEN + 10];
  ip_addr ip = IP6_NONE;

  switch (allowedip->family)
    {
    case AF_INET:
      ip = ipa_from_in4(allowedip->ip4);
      break;
    case AF_INET6:
      ip = ipa_from_in6(allowedip->ip6);
      break;
    default:
      return;
    }

  int res = bsnprintf(tmp, sizeof(tmp), "%I/%u", ip, allowedip->cidr);

  if (res < 0)
    {
      *size = 0;
      return;
    }

  wg_put_str("allowed_ip", tmp, buf, size);
}

static int
user_put_device(wg_device *dev, byte **buf, uint *size)
{
  wg_put_u16("set", 1, buf, size);
  if (dev->flags & WGDEVICE_HAS_PRIVATE_KEY)
    wg_put_key("private_key", dev->private_key, buf, size);
#if 0
  /* Setting listen_port causes dead-lock in wireguard-go. */
  if (dev->flags & WGDEVICE_HAS_LISTEN_PORT)
    wg_put_u16("listen_port", dev->listen_port, buf, size);
#endif
  wg_put_bool("replace_peers", dev->flags & WGDEVICE_REPLACE_PEERS, buf, size);

  wg_peer *peer = NULL;
  wg_for_each_peer(dev, peer)
    {
      wg_put_key("public_key", peer->public_key, buf, size);
      wg_put_endpoint(&peer->endpoint, buf, size);
      wg_put_bool("replace_allowed_ips", peer->flags & WGPEER_REPLACE_ALLOWEDIPS, buf, size);

      wg_allowedip *allowedip = NULL;
      wg_for_each_allowedip(peer, allowedip)
	{
	  wg_put_allowedip(allowedip, buf, size);
	}
    }
  wg_puts("\n", buf, size);

  if (*size > 0)
    return 0;
  else
    return -1;
}

int
wg_user_set_device(struct pool *pool,
		   const char *ifname,
		   struct wg_device *dev)
{
  char path[sizeof(struct sockaddr_un)];
  socklen_t pathlen = get_socket_path(ifname, path, sizeof(path));

  struct birdsock *sock = sk_new(pool);
  sock->rx_hook = user_rx_hook;
  sock->tx_hook = user_tx_hook;
  sock->err_hook = user_err_hook;
  int res = sk_connect_unix(sock, path, pathlen);
  DBG(L_TRACE "WG: socket %d %d %s", res, sock->fd, path);
  if (res < 0)
    {
      rfree(sock);
      return -1;
    }

  uint tbsize = 8192;
  sk_set_tbsize(sock, tbsize);
  sk_set_rbsize(sock, 16);
  byte *pos = sock->tbuf;
  uint size = tbsize;
  int len = user_put_device(dev, &pos, &size);

  DBG(L_TRACE "WG: put %d %s", size, sock->tbuf);

  if (len < 0)
    {
      rfree(sock);
      return -1;
    }
  sock->data = (void*)(uintptr_t)size;

  /* abort(); */
  return -1;
}