Debian Codegraph @

kiss/mkiss.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * mkiss.c
3 * Fake out AX.25 code into supporting dual port TNCS by routing serial
4 * port data to/from two pseudo ttys.
5 *
6 * Version 1.03
7 *
8 * N0BEL
9 * Kevin Uhlir
10 * [email protected]
11 *
12 * 1.01 12/30/95 Ron Curry - Fixed FD_STATE bug where FD_STATE was being used for
13 * three state machines causing spurious writes to wrong TNC port. FD_STATE is
14 * now used for real serial port, FD0_STATE for first pseudo tty, and FD1_STATE
15 * for second pseudo tty. This was an easy fix but a MAJOR bug.
16 *
17 * 1.02 3/1/96 Jonathan Naylor - Make hardware handshaking default to off.
18 * Allowed for true multiplexing of the data from the two pseudo ttys.
19 * Numerous formatting changes.
20 *
21 * 1.03 4/20/96 Tomi Manninen - Rewrote KISS en/decapsulation (much of the
22 * code taken from linux/drivers/net/slip.c). Added support for all the 16
23 * possible kiss ports and G8BPQ-style checksumming on ttyinterface. Now
24 * mkiss also sends a statistic report to stderr if a SIGUSR1 is sent to it.
25 *
26 * 1.04 25/5/96 Jonathan Naylor - Added check for UUCP style tty lock files.
27 * As they are now supported by kissattach as well.
28 *
29 * 1.05 20/8/96 Jonathan Naylor - Convert to becoming a daemon and added
30 * system logging.
31 *
32 * 1.06 23/11/96 Tomi Manninen - Added simple support for polled kiss.
33 *
34 * 1.07 12/24/97 Deti Fliegl - Added Flexnet/BayCom CRC mode with commandline
35 * parameter -f
36 *
37 * 1.08 xx/xx/99 Tom Mazouch - Adjustable poll interval
38 */
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <sys/time.h>
45 #include <time.h>
46 #include <sys/types.h>
47 #include <unistd.h>
48 #include <sys/ioctl.h>
49 #include <termios.h>
50 #include <fcntl.h>
51 #include <signal.h>
52 #include <ctype.h>
53 #include <syslog.h>
54 #include <limits.h>
55
56 #include <netax25/ttyutils.h>
57 #include <netax25/daemon.h>
58
59 #include <config.h>
60 #include <scm-version.h>
61
62 #define FLEX_CRC 2
63 #define G8BPQ_CRC 1
64
65 #define SIZE 4096
66
67 #define FEND 0300 /* Frame End (0xC0) */
68 #define FESC 0333 /* Frame Escape (0xDB) */
69 #define TFEND 0334 /* Transposed Frame End (0xDC) */
70 #define TFESC 0335 /* Transposed Frame Escape (0xDD) */
71
72 #define ACKREQ 0x0C
73 #define POLL 0x0E
74
75 /*
76 * Keep these off the stack.
77 */
78 static unsigned char ibuf[SIZE]; /* buffer for input operations */
79 static unsigned char obuf[SIZE]; /* buffer for kiss_tx() */
80
81 static int crc_errors;
82 static int invalid_ports;
83 static int return_polls ;
84
85 static char *usage_string = "usage: mkiss [-p interval] [-c] [-f] [-h] [-l] [-s speed] [-v] [-x <num_ptmx_devices>] ttyinterface pty ..\n";
86
87 static int dump_report = FALSE;
88
89 static int logging = FALSE;
90 static int crcflag = FALSE;
91 static int hwflag = FALSE;
92 static int pollspeed;
93
94 /* CRC-stuff */
95 typedef unsigned short int u16;
96 #define CRCTYP 0x20
97 static u16 crctab[256];
98
99 struct iface
100 {
101 char *name; /* Interface name (/dev/???) */
102 int fd; /* File descriptor */
103 int escaped; /* FESC received? */
104 u16 crc; /* Incoming frame crc */
105 unsigned char obuf[SIZE]; /* TX buffer */
106 unsigned char *optr; /* Next byte to transmit */
107 unsigned int errors; /* KISS protocol error count */
108 unsigned int nondata; /* Non-data frames rx count */
109 unsigned int rxpackets; /* RX frames count */
110 unsigned int txpackets; /* TX frames count */
111 unsigned long rxbytes; /* RX bytes count */
112 unsigned long txbytes; /* TX bytes count */
113 char namepts[PATH_MAX]; /* name of the unix98 pts slaves, which
114 * the client has to use */
115 };
116
117 static struct iface *tty;
118 static struct iface *pty[16];
119 static int numptys;
120
121 static void init_crc(void)
122 {
123 short int i, j;
124 u16 accum, data;
125
126 for (i = 0; i < 256; i++) { /* fill table with CRC of values... */
127 accum = 0xffff;
128 data = i;
129 for (j = 0; j < 8; ++j) {
130 if ((data^accum) & 0x0001)
131 /* if msb of data^accum is TRUE */
132 /* then shift and subtract poly */
133 accum = (accum >> 1) ^ 0x8408;
134 else
135 /* otherwise: transparent shift */
136 accum >>= 1;
137 data >>= 1; /* move up next bit for XOR */
138 }
139 crctab[i] = accum;
140 }
141 }
142
143 static int poll(int fd, int ports)
144 {
145 unsigned char buffer[3];
146 static int port;
147
148 buffer[0] = FEND;
149 buffer[1] = POLL | (port << 4);
150 buffer[2] = FEND;
151 if (write(fd, buffer, 3) == -1) {
152 perror("mkiss: poll: write");
153 return 1;
154 }
155 if (++port >= ports)
156 port = 0;
157 return 0;
158 }
159
160 static int put_ubyte(unsigned char* s, u16* crc, unsigned char c, int usecrc)
161 {
162 int len = 1;
163
164 if (c == FEND) {
165 *s++ = FESC;
166 *s++ = TFEND;
167 len++;
168 } else {
169 *s++ = c;
170 if (c == FESC) {
171 *s++ = TFESC;
172 len++;
173 }
174 }
175
176 switch (usecrc) {
177 case G8BPQ_CRC:
178 *crc ^= c; /* Adjust checksum */
179 break;
180 case FLEX_CRC:
181 *crc = (*crc<<8)^crctab[(*crc>>8)^((u16)((c)&255))];
182 break;
183 }
184
185 return len;
186 }
187
188 static int kiss_tx(int fd, int port, unsigned char *s, int len, int usecrc)
189 {
190 unsigned char *ptr = obuf;
191 unsigned char c, cmd;
192 u16 crc = 0;
193 int i;
194
195 cmd = s[0] & 0x0F;
196
197 /* Non-data frames don't get a checksum byte */
198 if (usecrc == G8BPQ_CRC && cmd != 0 && cmd != ACKREQ)
199 usecrc = FALSE;
200
201 /*
202 * Send an initial FEND character to flush out any
203 * data that may have accumulated in the receiver
204 * due to line noise.
205 */
206 *ptr++ = FEND;
207
208 if (usecrc == FLEX_CRC) {
209 crc = 0xffff;
210 ptr += put_ubyte(ptr, &crc, CRCTYP, usecrc);
211 c = *s++;
212 } else {
213 c = *s++;
214 c = (c & 0x0F) | (port << 4);
215 ptr += put_ubyte(ptr, &crc, c, usecrc);
216 }
217
218 /*
219 * For each byte in the packet, send the appropriate
220 * character sequence, according to the SLIP protocol.
221 */
222 for (i = 0; i < len - 1; i++)
223 ptr += put_ubyte(ptr, &crc, s[i], usecrc);
224
225 /*
226 * Now the checksum...
227 */
228 switch (usecrc) {
229 case G8BPQ_CRC:
230 c = crc & 0xFF;
231 ptr += put_ubyte(ptr, &crc, c, usecrc);
232 break;
233 case FLEX_CRC:
234 {
235 u16 u = crc;
236 ptr += put_ubyte(ptr, &crc, u / 256, usecrc);
237 ptr += put_ubyte(ptr, &crc, u & 255, usecrc);
238 }
239 break;
240 }
241
242 *ptr++ = FEND;
243 return write(fd, obuf, ptr - obuf);
244 }
245
246 static int kiss_rx(struct iface *ifp, unsigned char c, int usecrc)
247 {
248 int len;
249
250 switch (c) {
251 case FEND:
252 len = ifp->optr - ifp->obuf;
253
254 if (len != 0 && ifp->escaped) { /* protocol error... */
255 len = 0; /* ...drop frame */
256 ifp->errors++;
257 }
258
259 if (len != 0) {
260 switch (usecrc) {
261 case G8BPQ_CRC:
262 if ((ifp->obuf[0] & 0x0F) != 0) {
263 /*
264 * Non-data frames don't have checksum.
265 */
266 usecrc = 0;
267 if ((ifp->obuf[0] & 0x0F) == POLL) {
268 /* drop returned polls */
269 len = 0;
270 return_polls++;
271 } else
272 ifp->nondata++;
273 } else {
274 if ((ifp->crc & 0xFF) != 0) {
275 /* checksum failed... */
276 /* ...drop frame */
277 len = 0;
278 crc_errors++;
279 } else
280 /* delete checksum byte */
281 len--;
282 }
283 break;
284 case FLEX_CRC:
285 if (len > 14 && ifp->crc == 0x7070) {
286 len -= 2;
287 *ifp->obuf = 0;
288 } else {
289 len = 0;
290 crc_errors++;
291 }
292 break;
293 }
294 }
295
296 if (len != 0) {
297 ifp->rxpackets++;
298 ifp->rxbytes += len;
299 }
300
301 /*
302 * Clean up.
303 */
304 ifp->optr = ifp->obuf;
305 if (usecrc == FLEX_CRC)
306 ifp->crc = 0xffff;
307 else
308 ifp->crc = 0;
309 ifp->escaped = FALSE;
310 return len;
311 case FESC:
312 ifp->escaped = TRUE;
313 return 0;
314 case TFESC:
315 if (ifp->escaped) {
316 ifp->escaped = FALSE;
317 c = FESC;
318 }
319 break;
320 case TFEND:
321 if (ifp->escaped) {
322 ifp->escaped = FALSE;
323 c = FEND;
324 }
325 break;
326 default:
327 if (ifp->escaped) { /* protocol error... */
328 ifp->escaped = FALSE;
329 ifp->errors++;
330 }
331 break;
332 }
333
334 *ifp->optr++ = c;
335
336 switch (usecrc) {
337 case G8BPQ_CRC:
338 ifp->crc ^= c;
339 break;
340 case FLEX_CRC:
341 ifp->crc = (ifp->crc << 8) ^ crctab[(ifp->crc >> 8) ^ c];
342 break;
343 default:
344 break;
345 }
346
347 return 0;
348 }
349
350 static void sigterm_handler(int sig)
351 {
352 int i;
353
354 if (logging) {
355 syslog(LOG_INFO, "terminating on SIGTERM\n");
356 closelog();
357 }
358
359 tty_unlock(tty->name);
360 close(tty->fd);
361 free(tty);
362
363 for (i = 0; i < numptys; i++) {
364 if (pty[i]->fd == -1)
365 continue;
366 if (pty[i]->namepts[0] != '\0')
367 continue;
368 tty_unlock(pty[i]->name);
369 close(pty[i]->fd);
370 free(pty[i]);
371 }
372
373 exit(0);
374 }
375
376 static void sigusr1_handler(int sig)
377 {
378 signal(SIGUSR1, sigusr1_handler);
379 dump_report = TRUE;
380 }
381
382 static void report(void)
383 {
384 int i;
•385 time_t t;
386
387 time(&t);
388 syslog(LOG_INFO, "version %s.", FULL_VER);
389 syslog(LOG_INFO, "Status report at %s", ctime(&t));
390 syslog(LOG_INFO, "Hardware handshaking %sabled.",
391 hwflag ? "en" : "dis");
392 syslog(LOG_INFO, "G8BPQ checksumming %sabled.",
393 crcflag == G8BPQ_CRC ? "en" : "dis");
394 syslog(LOG_INFO, "FLEX checksumming %sabled.",
395 crcflag == FLEX_CRC ? "en" : "dis");
396
397 syslog(LOG_INFO, "polling %sabled.",
398 pollspeed ? "en" : "dis");
399 if (pollspeed)
400 syslog(LOG_INFO, "Poll interval %d00ms", pollspeed);
401 syslog(LOG_INFO, "ttyinterface is %s (fd=%d)", tty->name, tty->fd);
402 for (i = 0; i < numptys; i++)
403 syslog(LOG_INFO, "pty%d is %s (fd=%d)", i, pty[i]->name,
404 pty[i]->fd);
405 syslog(LOG_INFO, "Checksum errors: %d", crc_errors);
406 syslog(LOG_INFO, "Invalid ports: %d", invalid_ports);
407 syslog(LOG_INFO, "Returned polls: %d", return_polls);
408 syslog(LOG_INFO, "Interface TX frames TX bytes RX frames RX bytes Errors");
409 syslog(LOG_INFO, "%-11s %-9u %-9lu %-9u %-9lu %u",
410 tty->name,
411 tty->txpackets, tty->txbytes,
412 tty->rxpackets, tty->rxbytes,
413 tty->errors);
414 for (i = 0; i < numptys; i++) {
415 syslog(LOG_INFO, "%-11s %-9u %-9lu %-9u %-9lu %u",
416 pty[i]->name,
417 pty[i]->txpackets, pty[i]->txbytes,
418 pty[i]->rxpackets, pty[i]->rxbytes,
419 pty[i]->errors);
420 }
421 }
422
423 int main(int argc, char *argv[])
424 {
425 unsigned char *icp;
426 int topfd;
427 fd_set readfd;
428 struct timeval timeout, pollinterval;
429 int retval, i, size, len;
430 int speed = -1;
431 int ptmxdevices = 0;
432 char *npts;
433 int wrote_info = 0;
434
435 while ((size = getopt(argc, argv, "cfhlp:s:vx:")) != -1) {
436 switch (size) {
437 case 'c':
438 crcflag = G8BPQ_CRC;
439 break;
440 case 'f':
441 crcflag = FLEX_CRC;
442 break;
443 case 'h':
444 hwflag = TRUE;
445 break;
446 case 'l':
447 logging = TRUE;
448 break;
449 case 'p':
450 pollspeed = atoi(optarg);
451 pollinterval.tv_sec = pollspeed / 10;
452 pollinterval.tv_usec = (pollspeed % 10) * 100000L;
453 break;
454 case 's':
455 speed = atoi(optarg);
456 break;
457 case 'x':
458 ptmxdevices = atoi(optarg);
459 if (ptmxdevices < 1 || ptmxdevices > 16) {
460 fprintf(stderr, "mkiss: too %s devices\n", ptmxdevices < 1 ? "few" : "many");
461 return 1;
462 }
463 break;
464 case 'v':
465 printf("mkiss: %s\n", FULL_VER);
466 return 1;
467 case ':':
468 case '?':
469 fprintf(stderr, "%s", usage_string);
470 return 1;
471 }
472 }
473
474 if ((argc - optind) < 2 && ptmxdevices == 0) {
475 fprintf(stderr, "%s", usage_string);
476 return 1;
477 }
478
479 if ((argc - optind) < 1 && ptmxdevices > 0) {
480 fprintf(stderr, "%s", usage_string);
481 return 1;
482 }
483
484 numptys = argc - optind - 1;
485 if ((numptys + ptmxdevices) > 16) {
486 fprintf(stderr, "mkiss: max 16 pty interfaces allowed.\n");
487 return 1;
488 }
489
490 /*
491 * Check for lock files before opening any TTYs
492 */
493 if (tty_is_locked(argv[optind])) {
494 fprintf(stderr, "mkiss: tty %s is locked by another process\n", argv[optind]);
495 return 1;
496 }
497 for (i = 0; i < numptys; i++) {
498 if (!strcmp("/dev/ptmx", argv[optind + i + 1]))
499 continue;
500 if (!strcmp("none", argv[optind + i + 1]))
501 continue;
502 if (tty_is_locked(argv[optind + i + 1])) {
503 fprintf(stderr, "mkiss: pty %s is locked by another process\n", argv[optind + i + 1]);
504 return 1;
505 }
506 }
507
508 /*
509 * Open and configure the tty interface. Open() is
510 * non-blocking so it won't block regardless of the modem
511 * status lines.
512 */
513 tty = calloc(1, sizeof(struct iface));
514 if (tty == NULL) {
515 perror("mkiss: malloc");
516 return 1;
517 }
518
519 tty->fd = open(argv[optind], O_RDWR | O_NDELAY);
520 if (tty->fd == -1) {
521 perror("mkiss: open");
522 return 1;
523 }
524
525 tty->name = argv[optind];
526 tty_raw(tty->fd, hwflag);
527 if (speed != -1 && !tty_speed(tty->fd, speed)) {
528 close(tty->fd);
529 return 1;
530 }
531 tty->optr = tty->obuf;
532 topfd = tty->fd;
533 tty->namepts[0] = '\0';
534
535 /*
536 * Make it block again...
537 */
538 fcntl(tty->fd, F_SETFL, 0);
539
540 /*
541 * Open and configure the pty interfaces
542 */
543 for (i = 0; i < numptys+ptmxdevices; i++) {
544 static char name_ptmx[] = "/dev/ptmx";
545 char *pty_name = (i < numptys ? argv[optind+i+1] : name_ptmx);
546
547 pty[i] = calloc(1, sizeof(struct iface));
548 if (pty[i] == NULL) {
549 perror("mkiss: malloc");
550 return 1;
551 }
552 if (!strcmp(pty_name, "none")) {
553 pty[i]->fd = -1;
554 strcpy(pty[i]->namepts, "none");
555 } else {
556 pty[i]->fd = open(pty_name, O_RDWR);
557 if (pty[i]->fd == -1) {
558 perror("mkiss: open");
559 free(pty[i]);
560 pty[i] = NULL;
561 return 1;
562 }
563 tty_raw(pty[i]->fd, FALSE);
564 topfd = (pty[i]->fd > topfd) ? pty[i]->fd : topfd;
565 pty[i]->namepts[0] = '\0';
566 }
567 pty[i]->name = pty_name;
568 pty[i]->optr = pty[i]->obuf;
569 if (!strcmp(pty[i]->name, "/dev/ptmx")) {
570 /* get name of pts-device */
571 npts = ptsname(pty[i]->fd);
572 if (npts == NULL) {
573 fprintf(stderr, "mkiss: Cannot get name of pts-device.\n");
574 free(pty[i]);
575 pty[i] = NULL;
576 return 1;
577 }
578 strncpy(pty[i]->namepts, npts, PATH_MAX-1);
579 pty[i]->namepts[PATH_MAX-1] = '\0';
580
581 /* unlock pts-device */
582 if (unlockpt(pty[i]->fd) == -1) {
583 fprintf(stderr, "mkiss: Cannot unlock pts-device %s\n", pty[i]->namepts);
584 free(pty[i]);
585 pty[i] = NULL;
586 return 1;
587 }
588 if (wrote_info == 0)
589 printf("\nAwaiting client connects on:\n");
590 else
591 printf(" ");
592 printf("%s", pty[i]->namepts);
593 wrote_info = 1;
594 }
595 }
596
597 if (wrote_info > 0)
598 printf("\n");
599
600 numptys=numptys+ptmxdevices;
601
602 /*
603 * Now all the ports are open, lock them.
604 */
605 tty_lock(tty->name);
606 for (i = 0; i < numptys; i++) {
607 if (pty[i]->namepts[0] == '\0')
608 tty_lock(pty[i]->name);
609 }
610
611 if (logging) {
612 openlog("mkiss", LOG_PID, LOG_DAEMON);
613 syslog(LOG_INFO, "starting");
614 }
615
616 if (wrote_info > 0) {
617 fflush(stdout);
618 fflush(stderr);
619 close(0);
620 close(1);
621 close(2);
622 }
623
624 signal(SIGHUP, SIG_IGN);
625 signal(SIGUSR1, sigusr1_handler);
626 signal(SIGTERM, sigterm_handler);
627
628 if (!daemon_start(FALSE)) {
629 fprintf(stderr, "mkiss: cannot become a daemon\n");
630 return 1;
631 }
632
633 init_crc();
634 /*
635 * Loop until an error occurs on a read.
636 */
637 while (TRUE) {
638 FD_ZERO(&readfd);
639 FD_SET(tty->fd, &readfd);
640 for (i = 0; i < numptys; i++)
641 if (pty[i]->fd != -1)
642 FD_SET(pty[i]->fd, &readfd);
643
644 if (pollspeed)
645 timeout = pollinterval;
646
647 errno = 0;
648 retval = select(topfd + 1, &readfd, NULL, NULL, pollspeed ? &timeout : NULL);
649
650 if (retval == -1) {
651 if (dump_report) {
652 if (logging)
653 report();
654 dump_report = FALSE;
655 continue;
656 } else {
657 perror("mkiss: select");
658 continue;
659 }
660 }
661
662 /*
663 * Timer expired - let's poll...
664 */
665 if (retval == 0 && pollspeed) {
666 poll(tty->fd, numptys);
667 continue;
668 }
669
670 /*
671 * A character has arrived on the ttyinterface.
672 */
673 if (tty->fd > -1 && FD_ISSET(tty->fd, &readfd)) {
674 if ((size = read(tty->fd, ibuf, SIZE)) < 0 && errno != EINTR) {
675 if (logging)
676 syslog(LOG_ERR, "tty->fd: %m");
677 break;
678 }
679 for (icp = ibuf; size > 0; size--, icp++) {
680 len = kiss_rx(tty, *icp, crcflag);
681 if (len != 0) {
682 if ((i = (*tty->obuf & 0xF0) >> 4) < numptys) {
683 if (pty[i]->fd != -1) {
684 kiss_tx(pty[i]->fd, 0, tty->obuf, len, FALSE);
685 pty[i]->txpackets++;
686 pty[i]->txbytes += len;
687 }
688 } else
689 invalid_ports++;
690 if (pollspeed)
691 poll(tty->fd, numptys);
692 }
693 }
694 }
695
696 for (i = 0; i < numptys; i++) {
697 /*
698 * A character has arrived on pty[i].
699 */
700 if (pty[i]->fd > -1 && FD_ISSET(pty[i]->fd, &readfd)) {
701 if ((size = read(pty[i]->fd, ibuf, SIZE)) < 0 && errno != EINTR) {
702 if (logging)
703 syslog(LOG_ERR, "pty[%d]->fd: %m\n", i);
704 goto end;
705 }
706 for (icp = ibuf; size > 0; size--, icp++) {
707 len = kiss_rx(pty[i], *icp, FALSE);
708 if (len != 0) {
709 kiss_tx(tty->fd, i, pty[i]->obuf, len, crcflag);
710 tty->txpackets++;
711 tty->txbytes += len;
712 }
713 }
714 }
715 }
716 }
717
718 end:
719 if (logging)
720 closelog();
721
722 tty_unlock(tty->name);
723 close(tty->fd);
724 free(tty);
725
726 for (i = 0; i < numptys; i++) {
727 if (pty[i]->fd == -1)
728 continue;
729 if (pty[i]->namepts[0] != '\0')
730 continue;
731 tty_unlock(pty[i]->name);
732 close(pty[i]->fd);
733 free(pty[i]);
734 }
735
736 return 1;
737 }