forked from papazoga/tdclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
721 lines (602 loc) · 15 KB
/
client.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <linux/errqueue.h>
#include <netdb.h>
#include <netinet/in.h>
#include <resolv.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <time.h>
#include <signal.h>
#include <syslog.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include "td.h"
#define LOG_FACILITY LOG_DAEMON
#define BUFSIZE 2048
struct tunsess {
int sockfd;
struct sockaddr_in remote_sa;
struct sockaddr_in local_sa;
struct timespec last_seen;
socklen_t remote_sl;
socklen_t local_sl;
int local_port;
uint32_t remote_tunid;
uint32_t local_tunid;
char cookie[16];
char *ifname;
int pmtu;
char outbuf[BUFSIZE];
int outlen;
char inbuf[BUFSIZE];
int inlen;
int close_flag;
};
int dont_daemonize = 0;
int debug_flag = 0;
char *uuid = NULL;
char *local_ip = "0.0.0.0";
char *ifname = "l2tp0";
char *script = NULL;
int tunnel_id = 1;
int limit_down = -1;
struct tunsess *current;
void signal_handler(int);
int parse_broker(char *s, char **hostname, char **service)
{
char *p;
int len = 0;
/* find the separator */
for (p=s;*p && (*p) != ':';p++)
len++;
/* invalid without a separator */
if (*p == 0)
goto error_out;
*hostname = malloc(len);
strncpy(*hostname, s, len);
(*hostname)[len] = 0;
*service = strdup(++p);
return 0;
error_out:
return -1;
}
void dump_packet(char *buf, int len)
{
int i;
for (i=0;i<len;i++) {
printf ("%02X", (uint8_t)buf[i]);
if (i % 16 == 15)
fputc ('\n', stdout);
else
fputc (' ', stdout);
}
if (i % 16 != 0)
fputc('\n', stdout);
}
/*
* Subtract timespecs with carry.
*/
int timespec_subtract(struct timespec *result, struct timespec *x, struct timespec *y)
{
/* Perform the carry for the later subtraction by updating y. */
if (x->tv_nsec < y->tv_nsec) {
int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
y->tv_nsec -= 1000000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_nsec - y->tv_nsec > 1000000000) {
int nsec = (x->tv_nsec - y->tv_nsec) / 1000000000;
y->tv_nsec += 1000000000 * nsec;
y->tv_sec -= nsec;
}
/* Compute the time remaining to wait.
tv_nsec is certainly positive. */
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_nsec = x->tv_nsec - y->tv_nsec;
/* Return 1 if result is negative. */
return x->tv_sec < y->tv_sec;
}
/*
* Detect MTU using Linux error queues (not used).
*/
int detect_mtu(int sockfd)
{
struct sockaddr_in sa;
struct iovec iov;
struct msghdr msg;
struct cmsghdr *cmsg;
struct sock_extended_err *e = 0;
char cbuf[512];
char inbuf[512];
int res;
int mtu = -1;
iov.iov_base = inbuf; /* Should be unused */
iov.iov_len = 512;
msg.msg_name = &sa;
msg.msg_namelen = sizeof(sa);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_flags = 0;
msg.msg_control = cbuf;
msg.msg_controllen = sizeof(cbuf);
res = recvmsg(sockfd, &msg, MSG_ERRQUEUE);
if (res > 0) /* not sure if this can happen */
return -1;
if (res < 0 && errno == EAGAIN)
return -1;
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level != SOL_IP || cmsg->cmsg_type != IP_RECVERR)
continue;
e = (struct sock_extended_err *)CMSG_DATA(cmsg);
if (!e || e->ee_errno != EMSGSIZE)
continue;
mtu = e->ee_info;
}
return mtu;
}
/*
* Detect MTU via sockopt (not used).
*/
int get_mtu_sockopt(int sockfd, int *path_mtu)
{
socklen_t len;
len = sizeof(*path_mtu);
if (getsockopt(sockfd, SOL_IP, IP_MTU, path_mtu, &len) < 0) {
debug("unable to retrieve MTU\n");
return -1;
}
return 0;
}
int lookup(char *hostname, char *service, struct sockaddr_in *sa, socklen_t *sl, int flags)
{
struct addrinfo hints;
struct addrinfo *res = 0;
int s;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = flags;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
s = getaddrinfo(hostname, service, &hints, &res);
if (res) {
memcpy(sa, res->ai_addr, res->ai_addrlen);
*sl = res->ai_addrlen;
freeaddrinfo(res);
} else {
*sl = 0;
}
return s;
}
void print_help()
{
}
struct tunsess *ts_new(char *hostname, char *service)
{
struct tunsess *tun;
int r;
tun = malloc(sizeof(struct tunsess));
if (!tun)
return NULL;
tun->local_tunid = 1;
tun->close_flag = 0;
/* look up the local address */
if ((r = lookup(local_ip, NULL, &tun->local_sa, &tun->local_sl, AI_PASSIVE)) != 0) {
error("unable to resolve local endpoint: %s\n", gai_strerror(r));
return NULL;
}
/* look up the remote address */
if ((r=lookup(hostname, service, &tun->remote_sa, &tun->remote_sl, 0)) != 0) {
error("unable to perform resolution: %s\n", gai_strerror(r));
return NULL;
}
return tun;
}
int ts_socket_bind_connect(struct tunsess *tun)
{
int r;
int optval;
tun->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (tun->sockfd < 0)
return -1;
optval = IP_PMTUDISC_PROBE;
setsockopt(tun->sockfd, SOL_IP, IP_MTU_DISCOVER, &optval, sizeof(optval));
optval = 1;
setsockopt(tun->sockfd, SOL_IP, IP_RECVERR, &optval, sizeof(optval));
tun->local_sl = sizeof(tun->local_sa);
r = bind(tun->sockfd, (struct sockaddr *)&tun->local_sa, tun->local_sl);
if (r<0)
return r;
/* get the local port */
r = getsockname(tun->sockfd, (struct sockaddr *)&tun->local_sa, &tun->local_sl);
if (r<0)
return r;
r = connect(tun->sockfd, (struct sockaddr *)&tun->remote_sa, sizeof(tun->remote_sa));
tun->local_port = ntohs(tun->local_sa.sin_port);
return 0;
}
/*
* Compose a packet according to a format string.
*/
void ts_compose_packet(struct tunsess *tun, int type, char *fmt, ...)
{
char *p = tun->outbuf;
char *f, *s;
unsigned char *lenp;
va_list argp;
int slen, len;
uint8_t b;
uint16_t w;
uint32_t l;
va_start(argp, fmt);
*p++ = 0x80;
*p++ = 0x73;
*p++ = 0xa7;
*p++ = 0x01;
*p++ = type;
lenp = p++;
for (f=fmt;*f;f++) {
switch (*f) {
case 's': /* string */
s = va_arg(argp, char *);
slen = strlen(s);
*p++ = slen;
memcpy(p, s, slen);
p += slen;
break;
case 'b': /* byte */
b = va_arg(argp, int);
*p++ = b;
break;
case 'w': /* 16-bit word */
w = va_arg(argp, int);
*(uint16_t *)p = ntohs(w);
p += 2;
break;
case 'l': /* 32-bit word */
l = va_arg(argp, int);
*(uint32_t *)p = ntohl(l);
p += 4;
break;
case 'C': /* 8-byte cookie */
s = va_arg(argp, char *);
memcpy(p, s, 8);
p += 8;
break;
}
}
tun->outlen = p - tun->outbuf;
*lenp = tun->outlen - 6;
}
/*
* Verify that a received packet conforms
* to the protocol.
*/
int ts_verify_packet(struct tunsess *tun)
{
uint8_t *p = tun->inbuf;
if (*p++ == 0x80 &&
*p++ == 0x73 &&
*p++ == 0xa7 &&
*p++ == 0x01)
return 1;
else
return 0;
}
int ts_set_mtu(struct tunsess *tun, int mtu)
{
struct ifreq ifr;
int r;
debug("setting mtu of '%s' to %d\n", tun->ifname, mtu);
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, tun->ifname);
ifr.ifr_addr.sa_family = AF_INET;
ifr.ifr_mtu = mtu;
r = ioctl(tun->sockfd, SIOCSIFMTU, (caddr_t)&ifr);
if (r<0)
debug("failed: %s\n", strerror(errno));
}
int ts_send_packet(struct tunsess *tun)
{
int r;
int mtu;
r = sendto(tun->sockfd,
tun->outbuf,
tun->outlen,
0,
(struct sockaddr *)&tun->remote_sa,
tun->remote_sl);
if (r<0) {
if (errno == EMSGSIZE) {
mtu = detect_mtu(tun->sockfd);
if (mtu != tun->pmtu)
ts_set_mtu(tun, mtu-60);
debug("got MTU: %d\n", mtu);
} else {
debug("huh? errno==%d, '%s')\n", r, errno, strerror(errno));
}
}
return r;
}
int ts_recv_packet(struct tunsess *tun)
{
int r;
r = recv(tun->sockfd, tun->inbuf, BUFSIZE, MSG_DONTWAIT);
clock_gettime(CLOCK_MONOTONIC, &tun->last_seen);
if (r < 0)
return -1;
tun->inlen = r;
return 0;
}
/*
* Wait for an appropriate response packet for 'secs' seconds.
* Appropriateness is checked by calling callback(), which must return
* a non-zero value to cancel the select loop.
*
* Returns 0 on success, -1 on failure (timeout).
*/
int ts_packet_wait(struct tunsess *tun, int secs, int (*callback)(struct tunsess *))
{
fd_set rfds;
struct timespec ts_wait;
struct timespec ts_end;
struct timespec ts_current;
int r;
FD_ZERO(&rfds);
FD_SET(tun->sockfd, &rfds);
clock_gettime(CLOCK_MONOTONIC, &ts_end);
ts_end.tv_sec += secs;
do_select:
clock_gettime(CLOCK_MONOTONIC, &ts_current);
current = tun;
if (timespec_subtract(&ts_wait, &ts_end, &ts_current) == 1)
goto timed_out;
r = pselect(tun->sockfd+1, &rfds, NULL, NULL, &ts_wait, NULL);
if (r==1) {
ts_recv_packet(tun);
if (callback(tun) == 0)
return 0;
}
if (!tun->close_flag)
goto do_select;
timed_out:
return -1;
}
int cookie_cb(struct tunsess *tun)
{
if (P_TYPE(tun->inbuf) == PACKET_COOKIE) {
debug("received COOKIE 0x%16lx\n", *(uint64_t *)P_DATA(tun->inbuf));
memcpy(tun->cookie, P_DATA(tun->inbuf), 8);
tun->cookie[8] = 0;
return 0;
} else {
return -1;
}
}
#define COOKIE_TIMEOUT 5
#define COOKIE_RETRIES 3
int ts_get_cookie(struct tunsess *tun)
{
int tries;
ts_compose_packet(tun, PACKET_COOKIE, "C", "XXXXXXXX");
for (tries=0;tries<COOKIE_RETRIES;tries++) {
ts_send_packet(tun);
if (ts_packet_wait(tun, COOKIE_TIMEOUT, cookie_cb) == 0)
return 0;
}
return -1;
}
/*
* This callback responds to PMTU requests, too. It
* returns 0 only for teardowns.
*/
int keepalive_cb(struct tunsess *tun)
{
if (!ts_verify_packet(tun))
return 0;
switch (P_TYPE(tun->inbuf)) {
case PACKET_KEEPALIVE:
debug("received KEEPALIVE\n");
break;
case PACKET_PMTUD:
debug("received PMTUD with size %d\n", tun->inlen);
ts_compose_packet(tun, PACKET_PMTUD_ACK, "w", tun->inlen);
ts_send_packet(tun);
debug("sent PMTUD ACK\n");
break;
case PACKET_PMTUD_ACK:
debug("received PMTUD ACK\n");
break;
case PACKET_ERROR:
debug("received teardown request\n");
return 0;
break;
default:
debug("received unexpected packet of size %d\n", tun->inlen);
dump_packet(tun->inbuf, tun->inlen);
}
return -1;
}
/* These are in seconds */
#define KEEPALIVE_INTERVAL 3
#define KEEPALIVE_TIMELIMIT 30
void ts_keepalive(struct tunsess *tun)
{
struct timespec t;
for (;;) {
/* check MTU */
ts_compose_packet(tun, PACKET_KEEPALIVE, "");
tun->outlen = 2048;
ts_send_packet(tun);
if (ts_packet_wait(tun, KEEPALIVE_INTERVAL, keepalive_cb) == 0 || tun->close_flag)
return;
clock_gettime(CLOCK_MONOTONIC, &t);
timespec_subtract(&t, &t, &tun->last_seen);
debug("last seen: %d seconds ago.\n", t.tv_sec);
if (t.tv_sec >= KEEPALIVE_TIMELIMIT) {
error("tunnel timed out. shutting down.\n");
return;
}
ts_compose_packet(tun, PACKET_KEEPALIVE, "");
ts_send_packet(tun);
debug("sent KEEPALIVE\n");
}
}
#define TUNNEL_TIMEOUT 5
#define TUNNEL_RETRIES 3
int tunnel_cb(struct tunsess *tun)
{
if (P_TYPE(tun->inbuf) == PACKET_TUNNEL) {
tun->remote_tunid = ntohl(*(uint32_t *)P_DATA(tun->inbuf));
debug("received TUNNEL. Remote tunnel ID is %d.\n", tun->remote_tunid);
return 0;
} else
return -1;
}
int ts_close(struct tunsess *tun)
{
ts_compose_packet(tun, PACKET_ERROR, "");
ts_send_packet(tun);
close(tun->sockfd);
free(tun);
}
int ts_get_tunnel(struct tunsess *tun)
{
int tries;
int uuidlen = strlen(uuid);
uint16_t *tidp;
char *uuidp;
ts_compose_packet(tun, PACKET_PREPARE, "Csw", tun->cookie, uuid, htons(1));
for (tries=0;tries<TUNNEL_RETRIES;tries++) {
ts_send_packet(tun);
if (ts_packet_wait(tun, TUNNEL_TIMEOUT, tunnel_cb) == 0)
return 0;
}
return -1;
}
int ts_create_tunnel(struct tunsess *tun)
{
if (l2tp_create_tunnel(tun->local_tunid, tun->remote_tunid, tun->sockfd) < 0 ||
l2tp_create_session(tun->local_tunid, 1, 1, tun->ifname) < 0)
return -1;
return 0;
}
int ts_delete_tunnel(struct tunsess *tun)
{
if (l2tp_delete_session(tun->local_tunid, 1, 1, tun->ifname) < 0 ||
l2tp_delete_tunnel(tun->local_tunid, tun->remote_tunid, tun->sockfd) < 0)
return -1;
return 0;
}
void signal_handler(int signum)
{
switch(signum) {
case SIGINT:
current->close_flag = 1;
break;
}
}
int main(int argc, char **argv)
{
int c, r;
char *hostname, *service;
struct tunsess *tun;
struct sigaction sigact;
int local_tunid;
openlog("tunneldigger-client", 0, 0);
while ((c = getopt(argc, argv, "dhfu:l:b:p:i:s:t:L:")) != EOF) {
switch (c) {
case 'h': /* show help */
print_help();
exit(EXIT_SUCCESS);
break;
case 'f': /* don't daemonize */
dont_daemonize = 1;
break;
case 'd':
debug_flag = 1;
break;
case 'u': /* connection UUID */
uuid = strdup(optarg);
break;
case 'l': /* local IP address */
local_ip = strdup(optarg);
break;
case 'b': /* broker in <host>:<port> form */
if (parse_broker(optarg, &hostname, &service) != 0) {
fprintf(stderr, "invalid broker specification\n");
exit(EXIT_FAILURE);
}
break;
case 'i': /* tunnel interface name */
ifname = strdup(optarg);
break;
case 's': /* hook script */
script = strdup(optarg);
break;
case 't': /* tunnel id */
local_tunid = atoi(optarg);
break;
case 'L': /* limit bandwidth down */
limit_down = atoi(optarg);
break;
default:
fprintf(stderr, "use '-h' for help\n", c);
break;
}
}
l2tp_init();
if (!uuid) {
printf ("the uuid is required\n");
exit(EXIT_FAILURE);
}
tun = ts_new(hostname, service);
tun->local_tunid = local_tunid;
tun->ifname = strdup(ifname);
/* create and bind a socket, and connect it */
if ((r=ts_socket_bind_connect(tun)) != 0) {
error("unable to obtain bound socket\n");
exit(EXIT_FAILURE);
}
/* request a cookie */
if ((r=ts_get_cookie(tun)) != 0) {
error("unable to get cookie\n");
exit(EXIT_FAILURE);
}
/* request a tunnel */
if ((r=ts_get_tunnel(tun)) != 0) {
error("unable to get tunnel\n");
exit(EXIT_FAILURE);
}
/* create the tunnel */
if (ts_create_tunnel(tun) != 0) {
error("unable to create tunnel (is l2tp_eth loaded?)!\n");
exit(EXIT_FAILURE);
}
/* handle SIGINT */
sigact.sa_handler = signal_handler;
sigemptyset(&sigact.sa_mask);
if (sigaction(SIGINT, &sigact, NULL) < 0) {
error("couldn't set signal handler\n");
}
/* keep the tunnel alive, and respond to PMTU discovery requests */
ts_keepalive(tun);
error("closing tunnel\n");
if (ts_delete_tunnel(tun) != 0) {
error("unable to delete tunnel!\n");
exit(EXIT_FAILURE);
}
ts_close(tun);
}