-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhagridd.c
294 lines (239 loc) · 5.66 KB
/
hagridd.c
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
// SPDX-License-Identifier: Beerware
#define _GNU_SOURCE
#include <stdarg.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include "sha256.h"
__attribute__((noreturn))
static void die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n\n");
exit(EXIT_FAILURE);
}
static void usage(FILE *fp)
{
fprintf(fp, "usage: hagridd [-aBIND_ADDR] [-pBIND_PORT] <-cCONF_FILE>\n"
" -aBIND_ADDR IPv4 address to bind to (default INADDR_ANY)\n"
" -pBIND_PORT UDP port to bind to (default 9992)\n"
" -cCONF_FILE must contain line delimited peer specifications\n\n");
exit(fp == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
enum encap {
ENCAP_INVALID = 0,
ENCAP_NONE,
ENCAP_FOU,
ENCAP_GUE,
};
struct hagrid_msg {
uint32_t id;
struct in_addr addr;
char digest[32];
};
struct peer {
char ifname[IFNAMSIZ];
enum encap encap;
uint32_t id;
char secret[65];
};
static struct peer *peers;
static int npeers;
static enum encap strtoencap(const char *encap)
{
if (!strcmp(encap, "none"))
return ENCAP_NONE;
else if (!strcmp(encap, "fou"))
return ENCAP_FOU;
else if (!strcmp(encap, "gue"))
return ENCAP_GUE;
else
return ENCAP_INVALID;
}
static void add_peer(const char *ifname, enum encap encap, uint32_t id, const char *secret)
{
struct peer *p;
++npeers;
peers = realloc(peers, sizeof(*peers) * npeers);
if (!peers)
die("realloc");
p = &peers[npeers - 1];
strcpy(p->ifname, ifname);
p->encap = encap;
p->id = id;
strcpy(p->secret, secret);
printf("added peer %s:%i:%u\n", p->ifname, p->encap, p->id);
}
static struct peer *find_peer(uint32_t id)
{
int i;
for (i = 0; i < npeers; ++i)
if (peers[i].id == id)
return &peers[i];
return NULL;
}
static char *strspace(char *p)
{
while (*p && !isspace(*p))
++p;
return *p ? p : NULL;
}
static char *skip_spaces(char *p)
{
while (*p && isspace(*p))
++p;
return p;
}
static void read_peers(const char *path)
{
char *line;
ssize_t rd;
FILE *fp;
size_t n;
int i;
fp = fopen(path, "r");
if (!fp)
die("cannot open %s: %m", path);
line = NULL;
n = 0;
i = 1;
while ((rd = getline(&line, &n, fp)) > 0) {
char *ifname, *encapstr, *idstr, *secret, *end;
enum encap encap;
uint32_t id;
ifname = skip_spaces(line);
if (*ifname == '\0' || *ifname == '#') {
++i;
continue;
}
encapstr = strspace(ifname);
if (!encapstr || (encapstr - ifname >= IFNAMSIZ))
goto spec_fail;
*encapstr++ = '\0';
encapstr = skip_spaces(encapstr);
idstr = strspace(encapstr);
if (!idstr)
goto spec_fail;
*idstr++ = '\0';
encap = strtoencap(encapstr);
if (!encap)
goto spec_fail;
idstr = skip_spaces(idstr);
secret = strspace(idstr);
if (!secret)
goto spec_fail;
errno = 0;
id = strtoul(idstr, &end, 0);
if (errno || secret != end)
goto spec_fail;
*secret++ = '\0';
secret = skip_spaces(secret);
end = strspace(secret);
if (end)
*end = '\0';
if (strlen(secret) > 64)
goto spec_fail;
add_peer(ifname, encap, id, secret);
++i;
continue;
spec_fail:
die("wrong peer specification on %s:%i", path, i);
}
fclose(fp);
}
static void handle_change(struct hagrid_msg *msg, struct peer *p, const char *req_addr)
{
char cmd[256];
char *new_addr = inet_ntoa(msg->addr);
printf("valid msg from %s: peer %u (interface %s) changing remote address to %s\n",
req_addr, ntohl(msg->id), p->ifname, new_addr);
sprintf(cmd, "ip tunnel change %s remote %s", p->ifname, new_addr);
system(cmd);
}
int main(int argc, char **argv)
{
int sock, i, port;
struct sockaddr_in addr;
const char *addr_arg = "inaddr_any";
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = 0;
addr.sin_port = htons(9992);
for (i = 1; i < argc; ++i) {
const char *arg = argv[i] + 2;
if (argv[i][0] != '-')
usage(stderr);
switch (argv[i][1]) {
case 'p':
addr.sin_port = htons(atoi(arg));
break;
case 'a':
if (!inet_aton(arg, &addr.sin_addr))
die("Wrong address %s", arg);
addr_arg = arg;
break;
case 'c':
read_peers(arg);
break;
case 'h':
usage(stdout);
default:
usage(stderr);
}
}
if (!npeers)
die("no peers specified");
sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (sock < 0)
die("socket: %m");
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)))
die("cannot bind %s:%u: %m", addr_arg, ntohs(addr.sin_port));
printf("listening on %s:%u\n", addr_arg, ntohs(addr.sin_port));
while (1) {
struct hagrid_msg *msg;
struct sockaddr_in peer;
socklen_t slen = sizeof(peer);
char buf[64], peer_addr[22], digest[32];
SHA256_CTX ctx;
struct peer *p;
ssize_t rd;
rd = recvfrom(sock, buf, 64, MSG_TRUNC, (struct sockaddr *)&peer, &slen);
if (rd < 0) {
fprintf(stderr, "recvfrom: %m\n");
continue;
}
sprintf(peer_addr, "%s:%u", inet_ntoa(peer.sin_addr), ntohs(peer.sin_port));
if (rd != sizeof(*msg)) {
fprintf(stderr, "wrong message size from %s\n", peer_addr);
continue;
}
msg = (void *)buf;
p = find_peer(ntohl(msg->id));
if (!p) {
fprintf(stderr, "unknown peer id %u requested from %s\n", ntohl(msg->id),
peer_addr);
continue;
}
sha256_init(&ctx);
sha256_update(&ctx, buf, 8);
sha256_update(&ctx, p->secret, strlen(p->secret));
sha256_final(&ctx, digest);
if (memcmp(msg->digest, digest, 32)) {
fprintf(stderr, "wrong digest from %s\n", peer_addr);
continue;
}
/* if client didn't fill up address, fill peer address from socket */
if (!msg->addr.s_addr)
msg->addr.s_addr = peer.sin_addr.s_addr;
handle_change(msg, p, peer_addr);
}
}