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
|
/*
* mkiss.c
* Fake out AX.25 code into supporting dual port TNCS by routing serial
* port data to/from two pseudo ttys.
*
* Version 1.03
*
* N0BEL
* Kevin Uhlir
* kevinu@flochart.com
*
* 1.01 12/30/95 Ron Curry - Fixed FD_STATE bug where FD_STATE was being used for
* three state machines causing spurious writes to wrong TNC port. FD_STATE is
* now used for real serial port, FD0_STATE for first psuedo tty, and FD1_STATE
* for second psuedo tty. This was an easy fix but a MAJOR bug.
*
* 1.02 3/1/96 Jonathan Naylor - Make hardware handshaking default to off.
* Allowed for true multiplexing of the data from the two pseudo ttys.
* Numerous formatting changes.
*
* 1.03 4/20/96 Tomi Manninen - Rewrote KISS en/decapsulation (much of the
* code taken from linux/drivers/net/slip.c). Added support for all the 16
* possible kiss ports and G8BPQ-style checksumming on ttyinterface. Now
* mkiss also sends a statistic report to stderr if a SIGUSR1 is sent to it.
*
* 1.04 25/5/96 Jonathan Naylor - Added check for UUCP style tty lock files.
* As they are now supported by kissattach as well.
*
* 1.05 20/8/96 Jonathan Naylor - Convert to becoming a daemon and added
* system logging.
*
* 1.06 23/11/96 Tomi Manninen - Added simple support for polled kiss.
*
* 1.07 12/24/97 Deti Fliegl - Added Flexnet/BayCom CRC mode with commandline
* parameter -f
*
* 1.08 xx/xx/99 Tom Mazouch - Adjustable poll interval
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <fcntl.h>
#include <signal.h>
#include <ctype.h>
#include <syslog.h>
#include <netax25/ttyutils.h>
#include <netax25/daemon.h>
#include <config.h>
#define FLEX_CRC 2
#define G8BPQ_CRC 1
#define SIZE 4096
#define FEND 0300 /* Frame End (0xC0) */
#define FESC 0333 /* Frame Escape (0xDB) */
#define TFEND 0334 /* Transposed Frame End (0xDC) */
#define TFESC 0335 /* Transposed Frame Escape (0xDD) */
#define ACKREQ 0x0C
#define POLL 0x0E
/*
* Keep these off the stack.
*/
static unsigned char ibuf[SIZE]; /* buffer for input operations */
static unsigned char obuf[SIZE]; /* buffer for kiss_tx() */
static int crc_errors = 0;
static int invalid_ports = 0;
static int return_polls = 0;
static char *usage_string = "usage: mkiss [-p interval] [-c] [-f] [-h] [-l] [-s speed] [-v] ttyinterface pty ...\n";
static int dump_report = FALSE;
static int logging = FALSE;
static int crcflag = FALSE;
static int hwflag = FALSE;
static int pollspeed = 0;
/* CRC-stuff */
typedef unsigned short int u16;
#define CRCTYP 0x20
static u16 crctab[256];
struct iface
{
char *name; /* Interface name (/dev/???) */
int fd; /* File descriptor */
int escaped; /* FESC received? */
u16 crc; /* Incoming frame crc */
unsigned char obuf[SIZE]; /* TX buffer */
unsigned char *optr; /* Next byte to transmit */
unsigned int errors; /* KISS protocol error count */
unsigned int nondata; /* Non-data frames rx count */
unsigned int rxpackets; /* RX frames count */
unsigned int txpackets; /* TX frames count */
unsigned long rxbytes; /* RX bytes count */
unsigned long txbytes; /* TX bytes count */
};
static struct iface *tty = NULL;
static struct iface *pty[16] = { NULL };
static int numptys = 0;
static void init_crc(void)
{
short int i, j;
u16 accum, data;
for (i = 0; i < 256; i++) { /* fill table with CRC of values... */
accum = 0xffff;
data = i;
for (j = 0; j < 8; ++j) {
if ((data^accum) & 0x0001)
/* if msb of data^accum is TRUE */
/* then shift and subtract poly */
accum = (accum >> 1) ^ 0x8408;
else
/* otherwise: transparent shift */
accum >>= 1;
data >>= 1; /* move up next bit for XOR */
}
crctab[i] = accum;
}
}
static int poll(int fd, int ports)
{
char buffer[3];
static int port = 0;
buffer[0] = FEND;
buffer[1] = POLL | (port << 4);
buffer[2] = FEND;
if (write(fd, buffer, 3) == -1) {
perror("mkiss: poll: write");
return 1;
}
if (++port >= ports)
port = 0;
return 0;
}
static int put_ubyte(unsigned char* s, u16* crc, unsigned char c, int usecrc)
{
int len = 1;
if (c == FEND) {
*s++ = FESC;
*s++ = TFEND;
len++;
} else {
*s++ = c;
if (c == FESC) {
*s++ = TFESC;
len++;
}
}
switch (usecrc) {
case G8BPQ_CRC:
*crc ^= c; /* Adjust checksum */
break;
case FLEX_CRC:
*crc = (*crc<<8)^crctab[(*crc>>8)^((u16)((c)&255))];
break;
}
return len;
}
static int kiss_tx(int fd, int port, unsigned char *s, int len, int usecrc)
{
unsigned char *ptr = obuf;
unsigned char c, cmd;
u16 crc = 0;
int i;
cmd = s[0] & 0x0F;
/* Non-data frames don't get a checksum byte */
if (usecrc == G8BPQ_CRC && cmd != 0 && cmd != ACKREQ)
usecrc = FALSE;
/*
* Send an initial FEND character to flush out any
* data that may have accumulated in the receiver
* due to line noise.
*/
*ptr++ = FEND;
if (usecrc == FLEX_CRC) {
crc = 0xffff;
ptr += put_ubyte(ptr, &crc, CRCTYP, usecrc);
c = *s++;
} else {
c = *s++;
c = (c & 0x0F) | (port << 4);
ptr += put_ubyte(ptr, &crc, c, usecrc);
}
/*
* For each byte in the packet, send the appropriate
* character sequence, according to the SLIP protocol.
*/
for(i = 0; i < len - 1; i++)
ptr += put_ubyte(ptr, &crc, s[i], usecrc);
/*
* Now the checksum...
*/
switch (usecrc) {
case G8BPQ_CRC:
c = crc & 0xFF;
ptr += put_ubyte(ptr, &crc, c, usecrc);
break;
case FLEX_CRC:
{
u16 u = crc;
ptr += put_ubyte(ptr, &crc, u / 256, usecrc);
ptr += put_ubyte(ptr, &crc, u & 255, usecrc);
}
break;
}
*ptr++ = FEND;
return write(fd, obuf, ptr - obuf);
}
static int kiss_rx(struct iface *ifp, unsigned char c, int usecrc)
{
int len;
switch (c) {
case FEND:
len = ifp->optr - ifp->obuf;
if (len != 0 && ifp->escaped) { /* protocol error... */
len = 0; /* ...drop frame */
ifp->errors++;
}
if (len != 0) {
switch (usecrc) {
case G8BPQ_CRC:
if ((ifp->obuf[0] & 0x0F) != 0) {
/*
* Non-data frames don't have checksum.
*/
usecrc = 0;
if ((ifp->obuf[0] & 0x0F) == POLL) {
/* drop returned polls */
len = 0;
return_polls++;
} else
ifp->nondata++;
} else {
if ((ifp->crc & 0xFF) != 0) {
/* checksum failed... */
/* ...drop frame */
len = 0;
crc_errors++;
} else
/* delete checksum byte */
len--;
}
break;
case FLEX_CRC:
if (len > 14 && ifp->crc == 0x7070) {
len -= 2;
*ifp->obuf = 0;
} else {
len = 0;
crc_errors++;
}
break;
}
}
if (len != 0) {
ifp->rxpackets++;
ifp->rxbytes += len;
}
/*
* Clean up.
*/
ifp->optr = ifp->obuf;
if (usecrc == FLEX_CRC)
ifp->crc = 0xffff;
else
ifp->crc = 0;
ifp->escaped = FALSE;
return len;
case FESC:
ifp->escaped = TRUE;
return 0;
case TFESC:
if (ifp->escaped) {
ifp->escaped = FALSE;
c = FESC;
}
break;
case TFEND:
if (ifp->escaped) {
ifp->escaped = FALSE;
c = FEND;
}
break;
default:
if (ifp->escaped) { /* protocol error... */
ifp->escaped = FALSE;
ifp->errors++;
}
break;
}
*ifp->optr++ = c;
switch (usecrc) {
case G8BPQ_CRC:
ifp->crc ^= c;
break;
case FLEX_CRC:
ifp->crc = (ifp->crc << 8) ^ crctab[(ifp->crc >> 8) ^ c];
break;
default:
break;
}
return 0;
}
static void sigterm_handler(int sig)
{
int i;
if (logging) {
syslog(LOG_INFO, "terminating on SIGTERM\n");
closelog();
}
tty_unlock(tty->name);
close(tty->fd);
free(tty);
for (i = 0; i < numptys; i++) {
tty_unlock(pty[i]->name);
close(pty[i]->fd);
free(pty[i]);
}
exit(0);
}
static void sigusr1_handler(int sig)
{
signal(SIGUSR1, sigusr1_handler);
dump_report = TRUE;
}
static void report(void)
{
int i;
long t;
time(&t);
syslog(LOG_INFO, "version %s.", VERSION);
syslog(LOG_INFO, "Status report at %s", ctime(&t));
syslog(LOG_INFO, "Hardware handshaking %sabled.",
hwflag ? "en" : "dis");
syslog(LOG_INFO, "G8BPQ checksumming %sabled.",
crcflag == G8BPQ_CRC ? "en" : "dis");
syslog(LOG_INFO, "FLEX checksumming %sabled.",
crcflag == FLEX_CRC ? "en" : "dis");
syslog(LOG_INFO, "polling %sabled.",
pollspeed ? "en" : "dis");
if (pollspeed)
syslog(LOG_INFO, "Poll interval %d00ms", pollspeed);
syslog(LOG_INFO, "ttyinterface is %s (fd=%d)", tty->name, tty->fd);
for (i = 0; i < numptys; i++)
syslog(LOG_INFO, "pty%d is %s (fd=%d)", i, pty[i]->name,
pty[i]->fd);
syslog(LOG_INFO, "Checksum errors: %d", crc_errors);
syslog(LOG_INFO, "Invalid ports: %d", invalid_ports);
syslog(LOG_INFO, "Returned polls: %d", return_polls);
syslog(LOG_INFO, "Interface TX frames TX bytes RX frames RX bytes Errors");
syslog(LOG_INFO, "%-11s %-9u %-9lu %-9u %-9lu %u",
tty->name,
tty->txpackets, tty->txbytes,
tty->rxpackets, tty->rxbytes,
tty->errors);
for (i = 0; i < numptys; i++) {
syslog(LOG_INFO, "%-11s %-9u %-9lu %-9u %-9lu %u",
pty[i]->name,
pty[i]->txpackets, pty[i]->txbytes,
pty[i]->rxpackets, pty[i]->rxbytes,
pty[i]->errors);
}
return;
}
int main(int argc, char *argv[])
{
unsigned char *icp;
int topfd;
fd_set readfd;
struct timeval timeout, pollinterval;
int retval, i, size, len;
int speed = -1;
while ((size = getopt(argc, argv, "cfhlp:s:v")) != -1) {
switch (size) {
case 'c':
crcflag = G8BPQ_CRC;
break;
case 'f':
crcflag = FLEX_CRC;
break;
case 'h':
hwflag = TRUE;
break;
case 'l':
logging = TRUE;
break;
case 'p':
pollspeed = atoi(optarg);
pollinterval.tv_sec = pollspeed / 10;
pollinterval.tv_usec = (pollspeed % 10) * 100000L;
break;
case 's':
speed = atoi(optarg);
break;
case 'v':
printf("mkiss: %s\n", VERSION);
return 1;
case ':':
case '?':
fprintf(stderr, usage_string);
return 1;
}
}
if ((argc - optind) < 2) {
fprintf(stderr, usage_string);
return 1;
}
if ((numptys = argc - optind - 1) > 16) {
fprintf(stderr, "mkiss: max 16 pty interfaces allowed.\n");
return 1;
}
/*
* Check for lock files before opening any TTYs
*/
if (tty_is_locked(argv[optind])) {
fprintf(stderr, "mkiss: tty %s is locked by another process\n", argv[optind]);
return 1;
}
for (i = 0; i < numptys; i++) {
if (tty_is_locked(argv[optind + i + 1])) {
fprintf(stderr, "mkiss: tty %s is locked by another process\n", argv[optind + i + 1]);
return 1;
}
}
/*
* Open and configure the tty interface. Open() is
* non-blocking so it won't block regardless of the modem
* status lines.
*/
if ((tty = calloc(1, sizeof(struct iface))) == NULL) {
perror("mkiss: malloc");
return 1;
}
if ((tty->fd = open(argv[optind], O_RDWR | O_NDELAY)) == -1) {
perror("mkiss: open");
return 1;
}
tty->name = argv[optind];
tty_raw(tty->fd, hwflag);
if (speed != -1 && !tty_speed(tty->fd, speed)) {
close(tty->fd);
return 1;
}
tty->optr = tty->obuf;
topfd = tty->fd;
/*
* Make it block again...
*/
fcntl(tty->fd, F_SETFL, 0);
/*
* Open and configure the pty interfaces
*/
for (i = 0; i < numptys; i++) {
if ((pty[i] = calloc(1, sizeof(struct iface))) == NULL) {
perror("mkiss: malloc");
return 1;
}
if ((pty[i]->fd = open(argv[optind + i + 1], O_RDWR)) == -1) {
perror("mkiss: open");
return 1;
}
pty[i]->name = argv[optind + i + 1];
tty_raw(pty[i]->fd, FALSE);
pty[i]->optr = pty[i]->obuf;
topfd = (pty[i]->fd > topfd) ? pty[i]->fd : topfd;
}
signal(SIGHUP, SIG_IGN);
signal(SIGUSR1, sigusr1_handler);
signal(SIGTERM, sigterm_handler);
if (!daemon_start(FALSE)) {
fprintf(stderr, "mkiss: cannot become a daemon\n");
return 1;
}
/*
* Now all the ports are open, lock them.
*/
tty_lock(tty->name);
for (i = 0; i < numptys; i++)
tty_lock(pty[i]->name);
if (logging) {
openlog("mkiss", LOG_PID, LOG_DAEMON);
syslog(LOG_INFO, "starting");
}
init_crc();
/*
* Loop until an error occurs on a read.
*/
while (TRUE) {
FD_ZERO(&readfd);
FD_SET(tty->fd, &readfd);
for (i = 0; i < numptys; i++)
FD_SET(pty[i]->fd, &readfd);
if (pollspeed)
timeout = pollinterval;
errno = 0;
retval = select(topfd + 1, &readfd, NULL, NULL, pollspeed ? &timeout : NULL);
if (retval == -1) {
if (dump_report) {
if (logging)
report();
dump_report = FALSE;
continue;
} else {
perror("mkiss: select");
continue;
}
}
/*
* Timer expired - let's poll...
*/
if (retval == 0 && pollspeed) {
poll(tty->fd, numptys);
continue;
}
/*
* A character has arrived on the ttyinterface.
*/
if (FD_ISSET(tty->fd, &readfd)) {
if ((size = read(tty->fd, ibuf, SIZE)) < 0 && errno != EINTR) {
if (logging)
syslog(LOG_ERR, "tty->fd: %m");
break;
}
for (icp = ibuf; size > 0; size--, icp++) {
if ((len = kiss_rx(tty, *icp, crcflag)) != 0) {
if ((i = (*tty->obuf & 0xF0) >> 4) < numptys) {
kiss_tx(pty[i]->fd, 0, tty->obuf, len, FALSE);
pty[i]->txpackets++;
pty[i]->txbytes += len;
} else
invalid_ports++;
if (pollspeed)
poll(tty->fd, numptys);
}
}
}
for (i = 0; i < numptys; i++) {
/*
* A character has arrived on pty[i].
*/
if (FD_ISSET(pty[i]->fd, &readfd)) {
if ((size = read(pty[i]->fd, ibuf, SIZE)) < 0 && errno != EINTR) {
if (logging)
syslog(LOG_ERR, "pty[%d]->fd: %m\n", i);
goto end;
}
for (icp = ibuf; size > 0; size--, icp++) {
if ((len = kiss_rx(pty[i], *icp, FALSE)) != 0) {
kiss_tx(tty->fd, i, pty[i]->obuf, len, crcflag);
tty->txpackets++;
tty->txbytes += len;
}
}
}
}
}
end:
if (logging)
closelog();
tty_unlock(tty->name);
close(tty->fd);
free(tty);
for (i = 0; i < numptys; i++) {
tty_unlock(pty[i]->name);
close(pty[i]->fd);
free(pty[i]);
}
return 1;
}
|