-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtncattach.c
718 lines (627 loc) · 23 KB
/
tncattach.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
#include <stdlib.h>
#include <stdbool.h>
#include <signal.h>
#include <poll.h>
#include <argp.h>
#include <syslog.h>
#include <sys/stat.h>
#include <time.h>
#include "Constants.h"
#include "Serial.h"
#include "KISS.h"
#include "TCP.h"
#include "TAP.h"
#define BAUDRATE_DEFAULT 0
#define SERIAL_BUFFER_SIZE 512
#define IF_FD_INDEX 0
#define TNC_FD_INDEX 1
#define N_FDS 2
struct pollfd fds[N_FDS];
int attached_tnc;
int attached_if;
char if_name[IFNAMSIZ];
uint8_t serial_buffer[MTU_MAX];
uint8_t if_buffer[MTU_MAX];
bool verbose = false;
bool noipv6 = false;
bool noup = false;
bool daemonize = false;
bool set_ipv4 = false;
bool set_ipv6 = false;
bool set_linklocal = false;
bool set_netmask = false;
bool kiss_over_tcp = false;
char* ipv4_addr;
char* netmask;
char* ipv6_addr;
long ipv6_prefixLen;
char* tcp_host;
int tcp_port;
int mtu;
int device_type = IF_TUN;
char* id;
int id_interval = -1;
time_t last_id = 0;
bool tx_since_last_id = false;
void cleanup(void) {
if (kiss_over_tcp) {
close_tcp(attached_tnc);
} else {
close_port(attached_tnc);
}
close_tap(attached_if);
}
bool is_ipv6(uint8_t* frame) {
if (device_type == IF_TAP) {
if (frame[12] == 0x86 && frame[13] == 0xdd) {
return true;
} else {
return false;
}
} else if (device_type == IF_TUN) {
if (frame[2] == 0x86 && frame[3] == 0xdd) {
return true;
} else {
return false;
}
} else {
printf("Error: Unsupported interface type\r\n");
cleanup();
exit(1);
}
}
time_t time_now(void) {
time_t now = time(NULL);
if (now == -1) {
if (daemonize) {
syslog(LOG_ERR, "Could not get system time, exiting now");
} else {
printf("Error: Could not get system time, exiting now\r\n");
}
cleanup();
exit(1);
} else {
return now;
}
}
void transmit_id(void) {
time_t now = time(NULL);
int id_len = strlen(id);
if (verbose) {
if (!daemonize) {
printf("Transmitting %d bytes of identification data on %s: %s\r\n", id_len, if_name, id);
}
}
uint8_t* id_frame = malloc(strlen(id));
memcpy(id_frame, id, id_len);
kiss_write_frame(attached_tnc, id_frame, id_len);
last_id = now;
tx_since_last_id = false;
}
bool should_id(void) {
if (id_interval != -1) {
time_t now = time_now();
return now > last_id + id_interval;
} else {
return false;
}
}
void signal_handler(int signal) {
if (daemonize) syslog(LOG_NOTICE, "tncattach daemon exiting");
// Transmit final ID if necessary
if (id_interval != -1 && tx_since_last_id) transmit_id();
cleanup();
exit(0);
}
void read_loop(void) {
bool should_continue = true;
int min_frame_size;
if (device_type == IF_TAP) {
min_frame_size = ETHERNET_MIN_FRAME_SIZE;
} else if (device_type == IF_TUN) {
min_frame_size = TUN_MIN_FRAME_SIZE;
} else {
if (daemonize) {
syslog(LOG_ERR, "Unsupported interface type");
} else {
printf("Error: Unsupported interface type\r\n");
}
cleanup();
exit(1);
}
int poll_timeout = 1000;
while (should_continue) {
int poll_result = poll(fds, 2, poll_timeout);
if (poll_result != -1) {
if (poll_result == 0) {
// No resources are ready for reading,
// run scheduled tasks instead.
if (id_interval != -1 && tx_since_last_id) {
time_t now = time_now();
if (now > last_id + id_interval) transmit_id();
}
} else {
for (int fdi = 0; fdi < N_FDS; fdi++) {
if (fds[fdi].revents != 0) {
// Check for hangup event
if (fds[fdi].revents & POLLHUP) {
if (fdi == IF_FD_INDEX) {
if (daemonize) {
syslog(LOG_ERR, "Received hangup from interface");
} else {
printf("Received hangup from interface\r\n");
}
cleanup();
exit(1);
}
if (fdi == TNC_FD_INDEX) {
if (daemonize) {
syslog(LOG_ERR, "Received hangup from TNC");
} else {
printf("Received hangup from TNC\r\n");
}
cleanup();
exit(1);
}
}
// Check for error event
if (fds[fdi].revents & POLLERR) {
if (fdi == IF_FD_INDEX) {
if (daemonize) {
syslog(LOG_ERR, "Received error event from interface");
} else {
perror("Received error event from interface\r\n");
}
cleanup();
exit(1);
}
if (fdi == TNC_FD_INDEX) {
if (daemonize) {
syslog(LOG_ERR, "Received error event from TNC");
} else {
perror("Received error event from TNC\r\n");
}
cleanup();
exit(1);
}
}
// If data is ready, read it
if (fds[fdi].revents & POLLIN) {
if (fdi == IF_FD_INDEX) {
int if_len = read(attached_if, if_buffer, sizeof(if_buffer));
if (if_len > 0) {
if (if_len >= min_frame_size) {
if (!noipv6 || (noipv6 && !is_ipv6(if_buffer))) {
int tnc_written = kiss_write_frame(attached_tnc, if_buffer, if_len);
if (verbose && !daemonize) printf("Got %d bytes from interface, wrote %d bytes (KISS-framed and escaped) to TNC\r\n", if_len, tnc_written);
tx_since_last_id = true;
if (should_id()) transmit_id();
}
}
} else {
if (daemonize) {
syslog(LOG_ERR, "Could not read from network interface, exiting now");
} else {
printf("Error: Could not read from network interface, exiting now\r\n");
}
cleanup();
exit(1);
}
}
if (fdi == TNC_FD_INDEX) {
int tnc_len = read(attached_tnc, serial_buffer, sizeof(serial_buffer));
if (tnc_len > 0) {
for (int i = 0; i < tnc_len; i++) {
kiss_serial_read(serial_buffer[i]);
}
} else {
if (daemonize) {
syslog(LOG_ERR, "Could not read from TNC, exiting now");
} else {
printf("Error: Could not read from TNC, exiting now\r\n");
}
cleanup();
exit(1);
}
}
}
}
}
}
} else {
should_continue = false;
}
}
cleanup();
exit(1);
}
const char *argp_program_version = "tncattach 0.1.9";
const char *argp_program_bug_address = "<[email protected]>";
static char doc[] = "\r\nAttach TNC devices as system network interfaces\vTo attach the TNC connected to /dev/ttyUSB0 as an ethernet device with an MTU of 512 bytes and assign an IPv4 address, while filtering IPv6 traffic, use:\r\n\r\n\ttncattach /dev/ttyUSB0 115200 -m 512 -e --noipv6 --ipv4 10.0.0.1/24\r\n\r\nStation identification can be performed automatically to comply with Part 97 rules. See the README for a complete description. Use the --id and --interval options, which should commonly be set to your callsign, and 600 seconds.";
static char args_doc[] = "port baudrate";
static struct argp_option options[] = {
{ "mtu", 'm', "MTU", 0, "Specify interface MTU", 1},
{ "ethernet", 'e', 0, 0, "Create a full ethernet device", 2},
{ "ipv4", 'i', "IP_ADDRESS", 0, "Configure an IPv4 address on interface", 3},
{ "ipv6", '6', "IP6_ADDRESS", 0, "Configure an IPv6 address on interface", 4},
{ "ll", 'l', 0, 0, "Add a link-local Ipv6 address", 5},
{ "noipv6", 'n', 0, 0, "Filter IPv6 traffic from reaching TNC", 6},
{ "noup", 1, 0, 0, "Only create interface, don't bring it up", 7},
{ "kisstcp", 'T', 0, 0, "Use KISS over TCP instead of serial port", 8},
{ "tcphost", 'H', "TCP_HOST", 0, "Host to connect to when using KISS over TCP", 9},
{ "tcpport", 'P', "TCP_PORT", 0, "TCP port when using KISS over TCP", 10},
{ "interval", 't', "SECONDS", 0, "Maximum interval between station identifications", 11},
{ "id", 's', "CALLSIGN", 0, "Station identification data", 12},
{ "daemon", 'd', 0, 0, "Run tncattach as a daemon", 13},
{ "verbose", 'v', 0, 0, "Enable verbose output", 14},
{ 0 }
};
#define N_ARGS 2
struct arguments {
char *args[N_ARGS];
char *ipv4;
char *ipv6;
char *id;
bool valid_id;
int id_interval;
int baudrate;
int tcpport;
int mtu;
bool tap;
bool daemon;
bool verbose;
bool set_ipv4;
bool set_netmask;
bool set_ipv6;
bool link_local_v6;
bool set_netmask_v6;
bool noipv6;
bool noup;
bool kiss_over_tcp;
bool set_tcp_host;
bool set_tcp_port;
};
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
struct arguments *arguments = state->input;
switch (key) {
case 'v':
arguments->verbose = true;
break;
case 'e':
arguments->tap = true;
break;
case 'm':
arguments->mtu = atoi(arg);
if (arguments->mtu < MTU_MIN || arguments->mtu > MTU_MAX) {
printf("Error: Invalid MTU specified\r\n\r\n");
argp_usage(state);
}
if((arguments->set_ipv6 || arguments->link_local_v6) && arguments->mtu < 1280)
{
printf("IPv6 and/or link-local IPv6 was requested, but the MTU provided is lower than 1280\n");
exit(EXIT_FAILURE);
}
break;
case 't':
arguments->id_interval = atoi(arg);
if (arguments->id_interval < 0) {
printf("Error: Invalid identification interval specified\r\n\r\n");
argp_usage(state);
}
break;
case 's':
arguments->id = arg;
if (strlen(arg) < 1 || strlen(arg) > arguments->mtu) {
printf("Error: Invalid identification string specified\r\n\r\n");
argp_usage(state);
} else {
arguments->valid_id = true;
}
break;
case 'i':
arguments->ipv4 = arg;
arguments->set_ipv4 = true;
if (strchr(arg, '/')) {
char* net = strchr(arg, '/');
int pos = net-arg;
ipv4_addr = (char*)malloc(pos+1);
int mask = atoi(net+1);
strncpy(ipv4_addr, arg, pos);
switch (mask) {
case 0:
netmask = "0.0.0.0";
break;
case 1:
netmask = "128.0.0.0";
break;
case 2:
netmask = "192.0.0.0";
break;
case 3:
netmask = "224.0.0.0";
break;
case 4:
netmask = "240.0.0.0";
break;
case 5:
netmask = "248.0.0.0";
break;
case 6:
netmask = "252.0.0.0";
break;
case 7:
netmask = "254.0.0.0";
break;
case 8:
netmask = "255.0.0.0";
break;
case 9:
netmask = "255.128.0.0";
break;
case 10:
netmask = "255.192.0.0";
break;
case 11:
netmask = "255.224.0.0";
break;
case 12:
netmask = "255.240.0.0";
break;
case 13:
netmask = "255.248.0.0";
break;
case 14:
netmask = "255.252.0.0";
break;
case 15:
netmask = "255.254.0.0";
break;
case 16:
netmask = "255.255.0.0";
break;
case 17:
netmask = "255.255.128.0";
break;
case 18:
netmask = "255.255.192.0";
break;
case 19:
netmask = "255.255.224.0";
break;
case 20:
netmask = "255.255.240.0";
break;
case 21:
netmask = "255.255.248.0";
break;
case 22:
netmask = "255.255.252.0";
break;
case 23:
netmask = "255.255.254.0";
break;
case 24:
netmask = "255.255.255.0";
break;
case 25:
netmask = "255.255.255.128";
break;
case 26:
netmask = "255.255.255.192";
break;
case 27:
netmask = "255.255.255.224";
break;
case 28:
netmask = "255.255.255.240";
break;
case 29:
netmask = "255.255.255.248";
break;
case 30:
netmask = "255.255.255.252";
break;
case 31:
netmask = "255.255.255.254";
break;
case 32:
netmask = "255.255.255.255";
break;
default:
printf("Error: Invalid subnet mask specified\r\n");
cleanup();
exit(1);
}
arguments->set_netmask = true;
} else {
arguments->set_netmask = false;
ipv4_addr = (char*)malloc(strlen(arg)+1);
strcpy(ipv4_addr, arg);
}
break;
case '6':
if(arguments->noipv6)
{
perror("Sorry, but you had noipv6 set yet want to use ipv6?\n");
exit(EXIT_FAILURE);
}
char* ipPart_s = strtok(arg, "/");
char* prefixPart_s = strtok(NULL, "/");
printf("ipPart_s: %s\n", ipPart_s);
if(!prefixPart_s)
{
printf("No prefix length was provided\n");
exit(1);
}
printf("prefixPart_s: %s\n", prefixPart_s);
long prefixLen_l = strtol(prefixPart_s, NULL, 10); // TODO: Add handling here for errors (using errno)
if(prefixLen_l == 0) {
printf("Prefix length '%s' is not numeric\n", prefixPart_s);
exit(EXIT_FAILURE);
}
else if(!(prefixLen_l >= 0 && prefixLen_l <= 128))
{
printf("Prefix length '%s' is not within valid range of 0-128\n", prefixPart_s);
exit(EXIT_FAILURE);
}
arguments->ipv6 = ipPart_s;
arguments->set_ipv6 = true;
// Copy across global IPv6 address
ipv6_addr = malloc(strlen(arguments->ipv6)+1);
strcpy(ipv6_addr, arguments->ipv6);
// Set global IPv6 prefix length
ipv6_prefixLen = prefixLen_l;
printf("MTU was %d, setting to minimum of %d as is required for IPv6\n", arguments->mtu, 1280);
arguments->mtu = 1280;
break;
case 'l':
if(arguments->noipv6)
{
perror("Sorry, but you had noipv6 set yet want to use ipv6 link-local?\n");
exit(EXIT_FAILURE);
}
arguments->link_local_v6 = true;
printf("MTU was %d, setting to minimum of %d as is required for IPv6\n", arguments->mtu, 1280);
arguments->mtu = 1280;
break;
case 'n':
arguments->noipv6 = true;
if(arguments->set_ipv6)
{
printf("Requested no IPv6 yet you have set the IPv6 to '%s'\n", arguments->ipv6);
exit(1);
}
break;
case 'd':
arguments->daemon = true;
arguments->verbose = false;
break;
case 'T':
arguments->kiss_over_tcp = true;
break;
case 'H':
arguments->set_tcp_host = true;
tcp_host = (char*)malloc(strlen(arg)+1);
strcpy(tcp_host, arg);
break;
case 'P':
arguments->set_tcp_port = true;
tcp_port = atoi(arg);
break;
case 1:
arguments->noup = true;
break;
case ARGP_KEY_ARG:
// Check if there's now too many text arguments
if (state->arg_num >= N_ARGS) argp_usage(state);
// If not add to args
arguments->args[state->arg_num] = arg;
break;
case ARGP_KEY_END:
// Check if there's too few text arguments
if (!arguments->kiss_over_tcp && state->arg_num < N_ARGS) argp_usage(state);
// Check if text arguments were given when
// KISS over TCP was specified
if (arguments->kiss_over_tcp && state->arg_num != 0) argp_usage(state);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static void become_daemon() {
pid_t pid;
pid = fork();
if (pid < 0) {
perror("Fork failed");
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(0);
}
if (setsid() < 0) exit(1);
signal(SIGCHLD, signal_handler);
signal(SIGHUP, signal_handler);
pid = fork();
if (pid < 0) exit(1);
if (pid > 0) exit(0);
umask(0);
chdir("/");
openlog("tncattach", LOG_PID, LOG_DAEMON);
}
static struct argp argp = {options, parse_opt, args_doc, doc};
int main(int argc, char **argv) {
struct arguments arguments;
signal(SIGINT, signal_handler);
arguments.baudrate = BAUDRATE_DEFAULT;
arguments.mtu = MTU_DEFAULT;
arguments.tap = false;
arguments.verbose = false;
arguments.set_ipv4 = false;
arguments.set_netmask = false;
arguments.set_ipv6 = false;
arguments.link_local_v6 = false;
arguments.set_netmask_v6 = false;
arguments.noipv6 = false;
arguments.daemon = false;
arguments.noup = false;
arguments.id_interval = -1;
arguments.valid_id = false;
arguments.kiss_over_tcp = false;
argp_parse(&argp, argc, argv, 0, 0, &arguments);
if (arguments.kiss_over_tcp) kiss_over_tcp = true;
if (!kiss_over_tcp) {
arguments.baudrate = atoi(arguments.args[1]);
} else {
if (!(arguments.set_tcp_host && arguments.set_tcp_port)) {
if (!arguments.set_tcp_host) printf("Error: KISS over TCP was requested, but no host was specified\r\n");
if (!arguments.set_tcp_port) printf("Error: KISS over TCP was requested, but no port was specified\r\n");
exit(1);
}
}
if (arguments.daemon) daemonize = true;
if (arguments.verbose) verbose = true;
if (arguments.tap) device_type = IF_TAP;
if (arguments.noipv6) noipv6 = true;
if (arguments.set_ipv4) set_ipv4 = true;
if (arguments.set_netmask) set_netmask = true;
if (arguments.set_ipv6) set_ipv6 = true;
if (arguments.noup) noup = true;
mtu = arguments.mtu;
if (arguments.id_interval >= 0) {
if (!arguments.valid_id) {
printf("Error: Periodic identification requested, but no valid indentification data specified\r\n");
cleanup();
exit(1);
} else {
id_interval = arguments.id_interval;
id = malloc(strlen(arguments.id));
strcpy(id, arguments.id);
}
} else if (arguments.valid_id && arguments.id_interval == -1) {
printf("Error: Periodic identification requested, but no indentification interval specified\r\n");
cleanup();
exit(1);
}
attached_if = open_tap();
if (!arguments.kiss_over_tcp) {
attached_tnc = open_port(arguments.args[0]);
if (!setup_port(attached_tnc, arguments.baudrate)) {
printf("Error during serial port setup");
return 0;
}
} else {
attached_tnc = open_tcp(tcp_host, tcp_port);
}
printf("TNC interface configured as %s\r\n", if_name);
fds[IF_FD_INDEX].fd = attached_if;
fds[IF_FD_INDEX].events = POLLIN;
fds[TNC_FD_INDEX].fd = attached_tnc;
fds[TNC_FD_INDEX].events = POLLIN;
if (daemonize) {
become_daemon();
syslog(LOG_NOTICE, "tncattach daemon running");
}
read_loop();
return 0;
}