-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
458 lines (416 loc) · 11 KB
/
main.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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* This is an OpenBSD daemon for a modified version of the RIPv2
* protocol (RFC 2453). It is designed to handle route and
* tunnel maintenance for the AMPR amateur radio network
* (IPv4 44/8: http://www.ampr.org). Note that the AMPRNet is a
* mesh: each site sets up IPENCAP tunnels to (almost) all other
* sites.
*
* The daemon listens on a multicast socket for UDP datagrams
* directed to the RIP port. It discards packets that are not
* valid, authenticated RIP packets (though authentication is
* simply string comparison against a plaintext password: it is
* not particularly strong and could be trivially broken). It
* maintains an internal copy of the AMPRNet routing table as
* well as a set of active tunnels.
*
* After processing a RIP packet, the daemon walks through the
* routing table, looking for routes to expire. If a route
* expires it is noted for removal from the table. Expiration
* time is much greater than the expected interval between
* RIP broadcasts.
*
* Routes keep a reference to a tunnel. When a route is added
* that refers to an non-existent tunnel, the tunnel is created
* and set up. If a route referring to a tunnel is removed or
* changed to a different tunnel, the tunnel's reference count
* is decremented. If a tunnel's reference count drops to zero,
* it is torn down and removed.
*
* Each tunnel corresponds to a virtual IP encapsulation
* interface; see gif(4) for details. The daemon dynamically
* creates and destroys these interfaces as required. A bitmap
* of active interfaces is kept and the lowest unused interface
* number is always allocated when a new tunnel is created.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "dat.h"
#include "fns.h"
int init(int argc, char *argv[]);
void riptide(int sd);
void ripresponse(RIPResponse *response, time_t now);
Route *mkroute(uint32_t ipnet, uint32_t subnetmask, uint32_t gateway);
Tunnel *mktunnel(uint32_t local, uint32_t remote);
void alloctunif(Tunnel *tunnel, Bitvec *interfaces);
void unlinkroute(Tunnel *tunnel, Route *route);
void linkroute(Tunnel *tunnel, Route *route);
void walkexpired(time_t now);
void destroy(uint32_t key, size_t keylen, void *routep, void *unused);
void collapse(Tunnel *tunnel);
void expire(uint32_t key, size_t keylen, void *routep, void *statep);
void usage(const char *restrict prog);
enum {
CIDR_HOST = 32,
RIPV2_PORT = 520,
DEFAULT_ROUTE_TABLE = 44,
//TIMEOUT = 7*24*60*60, // 7 days
TIMEOUT = 15*60, // 15 minutes.
};
const char *DEFAULT_LOCAL_ADDRESS = "23.30.150.141";
const char *DEFAULT_LOCAL_44ADDRESS = "44.44.48.1";
const char *DEFAULT_GATEWAY_ADDRESS = "169.228.34.84";
const char *RIPV2_GROUP = "224.0.0.9";
const char *PASSWORD = "pLaInTeXtpAsSwD";
IPMap *ignoreroutes;
IPMap *routes;
IPMap *tunnels;
Bitvec *interfaces;
Bitvec *staticinterfaces;
const char *prog;
uint32_t localaddr;
uint32_t local44addr;
uint32_t defgwaddr;
int routedomain;
int tunneldomain;
int lowgif;
int
main(int argc, char *argv[])
{
int sd;
sd = init(argc, argv);
for (;;)
riptide(sd);
close(sd);
return 0;
}
int
init(int argc, char *argv[])
{
const char *localip, *local44;
char *iface = "*";
char *slash;
int sd, ch, daemonize;
struct in_addr addr;
slash = strrchr(argv[0], '/');
prog = (slash == NULL) ? argv[0] : slash + 1;
daemonize = 1;
interfaces = mkbitvec();
staticinterfaces = mkbitvec();
routedomain = DEFAULT_ROUTE_TABLE;
tunneldomain = DEFAULT_ROUTE_TABLE;
local44 = DEFAULT_LOCAL_44ADDRESS;
localip = DEFAULT_LOCAL_ADDRESS;
routes = mkipmap();
tunnels = mkipmap();
while ((ch = getopt(argc, argv, "dD:T:L:i:I:s:")) != -1) {
switch (ch) {
case 'd':
daemonize = 0;
break;
case 'T':
tunneldomain = strnum(optarg);
break;
case 'D':
routedomain = strnum(optarg);
break;
case 'l':
local44 = optarg;
break;
case 'L':
localip = optarg;
break;
case 'i':
iface = optarg;
break;
case 'I': {
static void *IGNORE = (void *)0x10; // Arbitrary.
uint32_t iroute;
size_t icidr;
slash = strchr(optarg, '/');
if (slash == NULL)
fatal("Bad route (use CIDR): %s\n", optarg);
*slash++ = '\0';
iroute = strnum(optarg);
icidr = strnum(slash);
ipmapinsert(ignoreroutes, iroute, icidr, IGNORE);
break;
}
case 's': {
unsigned int ifnum = strnum(optarg);
bitset(staticinterfaces, ifnum);
bitset(interfaces, ifnum);
break;
}
case '?':
case 'h':
default:
usage(prog);
}
}
initsys(routedomain);
sd = initsock(iface, RIPV2_GROUP, RIPV2_PORT, routedomain);
memset(&addr, 0, sizeof(addr));
inet_pton(AF_INET, localip, &addr);
localaddr = ntohl(addr.s_addr);
memset(&addr, 0, sizeof(addr));
inet_pton(AF_INET, local44, &addr);
local44addr = ntohl(addr.s_addr);
memset(&addr, 0, sizeof(addr));
inet_pton(AF_INET, DEFAULT_GATEWAY_ADDRESS, &addr);
defgwaddr = ntohl(addr.s_addr);
if (daemonize) {
const int chdiryes = 0;
const int closeyes = 0;
daemon(chdiryes, closeyes);
}
initlog();
return sd;
}
void
riptide(int sd)
{
struct sockaddr *rem;
struct sockaddr_in remote;
socklen_t remotelen;
ssize_t n;
time_t now;
RIPPacket pkt;
octet packet[IP_MAXPACKET];
memset(&remote, 0, sizeof(remote));
remotelen = 0;
rem = (struct sockaddr *)&remote;
n = recvfrom(sd, packet, sizeof(packet), 0, rem, &remotelen);
if (n < 0)
fatal("socket error");
memset(&pkt, 0, sizeof(pkt));
if (parserippkt(packet, n, &pkt) < 0) {
error("packet parse error\n");
return;
}
if (verifyripauth(&pkt, PASSWORD) < 0) {
error("packet authentication failed\n");
return;
}
now = time(NULL);
for (int k = 0; k < pkt.nresponse; k++) {
RIPResponse response;
memset(&response, 0, sizeof(response));
if (parseripresponse(&pkt, k, &response) < 0) {
notice("bad response, index %d\n", k);
continue;
}
ripresponse(&response, now);
}
walkexpired(now);
}
void
ripresponse(RIPResponse *response, time_t now)
{
Route *route;
Tunnel *tunnel;
size_t cidr;
char proute[INET_ADDRSTRLEN], gw[INET_ADDRSTRLEN];
cidr = netmask2cidr(response->subnetmask);
ipaddrstr(response->ipaddr, proute);
ipaddrstr(response->nexthop, gw);
if (response->ipaddr & ~response->subnetmask)
error("route ipaddr %s has more bits than netmask, %zu",
proute, cidr);
response->ipaddr &= response->subnetmask;
if (response->nexthop == localaddr) {
notice("skipping route for %s/%zu to local address",
proute, cidr);
return;
}
if ((response->nexthop & response->ipaddr) == response->nexthop) {
error("skipping gateway inside of subnet (%s/%zu -> %s)",
proute, cidr, gw);
return;
}
tunnel = ipmapfind(tunnels, response->nexthop, CIDR_HOST);
if (tunnel == NULL && defgwaddr != response->nexthop) {
tunnel = mktunnel(localaddr, response->nexthop);
alloctunif(tunnel, interfaces);
uptunnel(tunnel, routedomain, tunneldomain, local44addr);
ipmapinsert(tunnels, response->nexthop, CIDR_HOST, tunnel);
}
route = ipmapfind(routes, response->ipaddr, cidr);
if (route == NULL) {
route = mkroute(
response->ipaddr,
response->subnetmask,
response->nexthop);
ipmapinsert(routes, route->ipnet, cidr, route);
info("Added route %s/%zu -> %s", proute, cidr, gw);
}
// The route is new or moved to a different tunnel.
if (route->tunnel != tunnel) {
if (route->tunnel == NULL)
addroute(route, tunnel, routedomain);
else
chroute(route, tunnel, routedomain);
unlinkroute(tunnel, route);
unlinkroute(route->tunnel, route);
collapse(route->tunnel);
linkroute(tunnel, route);
}
route->expires = now + TIMEOUT;
debug("RIPv2 response: %s/%zu -> %s", proute, cidr, gw);
}
Route *
mkroute(uint32_t ipnet, uint32_t subnetmask, uint32_t gateway)
{
Route *route;
route = calloc(1, sizeof(*route));
if (route == NULL)
fatal("malloc");
route->ipnet = ipnet;
route->subnetmask = subnetmask;
route->gateway = gateway;
return route;
}
Tunnel *
mktunnel(uint32_t local, uint32_t remote)
{
Tunnel *tunnel;
tunnel = calloc(1, sizeof(*tunnel));
if (tunnel == NULL)
fatal("malloc");
tunnel->local = local;
tunnel->remote = remote;
return tunnel;
}
void
alloctunif(Tunnel *tunnel, Bitvec *interfaces)
{
size_t ifnum;
ifnum = nextbit(interfaces);
tunnel->ifnum = ifnum;
snprintf(tunnel->ifname, sizeof(tunnel->ifname), "gif%zu", ifnum);
bitset(interfaces, ifnum);
info("Allocating tunnel interface %s", tunnel->ifname);
}
void
unlinkroute(Tunnel *tunnel, Route *route)
{
if (tunnel == NULL)
return;
for (Route *prev = NULL, *tmp = tunnel->routes;
tmp != NULL;
prev = tmp, tmp = tmp->rnext)
{
if (route->ipnet == tmp->ipnet &&
route->subnetmask == tmp->subnetmask)
{
if (prev == NULL)
tunnel->routes = tmp->rnext;
else
prev->rnext = tmp->rnext;
route->gateway = 0;
--tunnel->nref;
break;
}
}
}
void
linkroute(Tunnel *tunnel, Route *route)
{
route->rnext = tunnel->routes;
tunnel->routes = route;
route->tunnel = tunnel;
route->gateway = tunnel->remote;
++tunnel->nref;
}
typedef struct WalkState WalkState;
struct WalkState {
time_t now;
IPMap *deleting;
};
void
walkexpired(time_t now)
{
WalkState state = { now, NULL };
ipmapdo(routes, expire, &state);
if (state.deleting != NULL) {
ipmapdo(state.deleting, destroy, NULL);
freeipmap(state.deleting, free);
}
}
void
expire(uint32_t key, size_t keylen, void *routep, void *statep)
{
Route *route = routep;
WalkState *state = statep;
size_t cidr;
char proute[INET_ADDRSTRLEN], gw[INET_ADDRSTRLEN];
if (route->expires > state->now)
return;
if (state->deleting == NULL)
state->deleting = mkipmap();
cidr = netmask2cidr(route->subnetmask);
assert(cidr == keylen);
ipaddrstr(route->ipnet, proute);
ipaddrstr(route->gateway, gw);
info("Expiring route %s/%zu -> %s", proute, cidr, gw);
ipmapinsert(state->deleting, key, keylen, route);
}
void
destroy(uint32_t key, size_t keylen, void *routep, void *unused)
{
Route *route = routep;
Tunnel *tunnel;
void *datum;
size_t cidr;
char proute[INET_ADDRSTRLEN], gw[INET_ADDRSTRLEN];
(void)unused;
if (route == NULL)
return;
cidr = netmask2cidr(route->subnetmask);
assert(cidr == keylen);
ipaddrstr(route->ipnet, proute);
ipaddrstr(route->gateway, gw);
info("Destroying route %s/%zu -> %s", proute, cidr, gw);
datum = ipmapremove(routes, key, keylen);
assert(datum == route);
tunnel = route->tunnel;
assert(tunnel != NULL);
unlinkroute(tunnel, route);
rmroute(route, routedomain);
collapse(tunnel);
}
void
collapse(Tunnel *tunnel)
{
if (tunnel == NULL)
return;
assert(tunnel->nref >= 0);
if (tunnel->nref == 0) {
void *datum = ipmapremove(tunnels, tunnel->remote, CIDR_HOST);
assert(datum == tunnel);
info("Tearing down tunnel interface %s", tunnel->ifname);
downtunnel(tunnel);
bitclr(interfaces, tunnel->ifnum);
free(tunnel);
}
}
void
usage(const char *restrict prog)
{
fprintf(stderr,
"Usage: %s [ -d ] [ -T rtable ] [ -L local_ip ] "
"[ -I ignore ] [ -s static_ifnum ]\n",
prog);
exit(EXIT_FAILURE);
}