tunslip6: support MTU configuration, XON/XOFF flow control, and baudrates up

to 1000000 mbps
This commit is contained in:
Simon Duquennoy 2015-08-18 14:57:08 +02:00
parent 9171ba17fc
commit 55d81ddd3a

View File

@ -62,7 +62,7 @@ const char *netmask;
int slipfd = 0; int slipfd = 0;
uint16_t basedelay=0,delaymsec=0; uint16_t basedelay=0,delaymsec=0;
uint32_t startsec,startmsec,delaystartsec,delaystartmsec; uint32_t startsec,startmsec,delaystartsec,delaystartmsec;
int timestamp = 0, flowcontrol=0, showprogress=0; int timestamp = 0, flowcontrol=0, showprogress=0, flowcontrol_xonxoff=0;
int ssystem(const char *fmt, ...) int ssystem(const char *fmt, ...)
__attribute__((__format__ (__printf__, 1, 2))); __attribute__((__format__ (__printf__, 1, 2)));
@ -75,6 +75,10 @@ void slip_send_char(int fd, unsigned char c);
char tundev[1024] = { "" }; char tundev[1024] = { "" };
/* IPv6 required minimum MTU */
#define MIN_DEVMTU 1500
int devmtu = MIN_DEVMTU;
int int
ssystem(const char *fmt, ...) __attribute__((__format__ (__printf__, 1, 2))); ssystem(const char *fmt, ...) __attribute__((__format__ (__printf__, 1, 2)));
@ -96,6 +100,10 @@ ssystem(const char *fmt, ...)
#define SLIP_ESC_END 0334 #define SLIP_ESC_END 0334
#define SLIP_ESC_ESC 0335 #define SLIP_ESC_ESC 0335
#define SLIP_ESC_XON 0336
#define SLIP_ESC_XOFF 0337
#define XON 17
#define XOFF 19
/* get sockaddr, IPv4 or IPv6: */ /* get sockaddr, IPv4 or IPv6: */
void * void *
@ -287,6 +295,12 @@ serial_to_tun(FILE *inslip, int outfd)
case SLIP_ESC_ESC: case SLIP_ESC_ESC:
c = SLIP_ESC; c = SLIP_ESC;
break; break;
case SLIP_ESC_XON:
c = XON;
break;
case SLIP_ESC_XOFF:
c = XOFF;
break;
} }
/* FALLTHROUGH */ /* FALLTHROUGH */
default: default:
@ -330,6 +344,22 @@ slip_send_char(int fd, unsigned char c)
slip_send(fd, SLIP_ESC); slip_send(fd, SLIP_ESC);
slip_send(fd, SLIP_ESC_ESC); slip_send(fd, SLIP_ESC_ESC);
break; break;
case XON:
if(flowcontrol_xonxoff) {
slip_send(fd, SLIP_ESC);
slip_send(fd, SLIP_ESC_XON);
} else {
slip_send(fd, c);
}
break;
case XOFF:
if(flowcontrol_xonxoff) {
slip_send(fd, SLIP_ESC);
slip_send(fd, SLIP_ESC_XOFF);
} else {
slip_send(fd, c);
}
break;
default: default:
slip_send(fd, c); slip_send(fd, c);
break; break;
@ -415,6 +445,22 @@ write_to_serial(int outfd, void *inbuf, int len)
slip_send(outfd, SLIP_ESC); slip_send(outfd, SLIP_ESC);
slip_send(outfd, SLIP_ESC_ESC); slip_send(outfd, SLIP_ESC_ESC);
break; break;
case XON:
if(flowcontrol_xonxoff) {
slip_send(outfd, SLIP_ESC);
slip_send(outfd, SLIP_ESC_XON);
} else {
slip_send(outfd, p[i]);
}
break;
case XOFF:
if(flowcontrol_xonxoff) {
slip_send(outfd, SLIP_ESC);
slip_send(outfd, SLIP_ESC_XOFF);
} else {
slip_send(outfd, p[i]);
}
break;
default: default:
slip_send(outfd, p[i]); slip_send(outfd, p[i]);
break; break;
@ -467,6 +513,12 @@ stty_telos(int fd)
tty.c_cflag |= CRTSCTS; tty.c_cflag |= CRTSCTS;
else else
tty.c_cflag &= ~CRTSCTS; tty.c_cflag &= ~CRTSCTS;
tty.c_iflag &= ~IXON;
if(flowcontrol_xonxoff) {
tty.c_iflag |= IXOFF | IXANY;
} else {
tty.c_iflag &= ~IXOFF & ~IXANY;
}
tty.c_cflag &= ~HUPCL; tty.c_cflag &= ~HUPCL;
tty.c_cflag &= ~CLOCAL; tty.c_cflag &= ~CLOCAL;
@ -609,7 +661,7 @@ ifconf(const char *tundev, const char *ipaddr)
{ {
#ifdef linux #ifdef linux
if (timestamp) stamptime(); if (timestamp) stamptime();
ssystem("ifconfig %s inet `hostname` up", tundev); ssystem("ifconfig %s inet `hostname` mtu %d up", tundev, devmtu);
if (timestamp) stamptime(); if (timestamp) stamptime();
ssystem("ifconfig %s add %s", tundev, ipaddr); ssystem("ifconfig %s add %s", tundev, ipaddr);
@ -670,7 +722,7 @@ ifconf(const char *tundev, const char *ipaddr)
prefix = "64"; prefix = "64";
} }
if (timestamp) stamptime(); if (timestamp) stamptime();
ssystem("ifconfig %s inet6 up", tundev ); ssystem("ifconfig %s inet6 mtu %d up", tundev, devmtu);
if (timestamp) stamptime(); if (timestamp) stamptime();
ssystem("ifconfig %s inet6 %s add", tundev, ipaddr ); ssystem("ifconfig %s inet6 %s add", tundev, ipaddr );
if (timestamp) stamptime(); if (timestamp) stamptime();
@ -679,7 +731,7 @@ ifconf(const char *tundev, const char *ipaddr)
} }
#else #else
if (timestamp) stamptime(); if (timestamp) stamptime();
ssystem("ifconfig %s inet `hostname` %s up", tundev, ipaddr); ssystem("ifconfig %s inet `hostname` %s mtu %d up", tundev, ipaddr, devmtu);
if (timestamp) stamptime(); if (timestamp) stamptime();
ssystem("sysctl -w net.inet.ip.forwarding=1"); ssystem("sysctl -w net.inet.ip.forwarding=1");
#endif /* !linux */ #endif /* !linux */
@ -708,7 +760,7 @@ main(int argc, char **argv)
prog = argv[0]; prog = argv[0];
setvbuf(stdout, NULL, _IOLBF, 0); /* Line buffered output. */ setvbuf(stdout, NULL, _IOLBF, 0); /* Line buffered output. */
while((c = getopt(argc, argv, "B:HILPhs:t:v::d::a:p:T")) != -1) { while((c = getopt(argc, argv, "B:HILPhXM:s:t:v::d::a:p:T")) != -1) {
switch(c) { switch(c) {
case 'B': case 'B':
baudrate = atoi(optarg); baudrate = atoi(optarg);
@ -718,10 +770,20 @@ main(int argc, char **argv)
flowcontrol=1; flowcontrol=1;
break; break;
case 'X':
flowcontrol_xonxoff=1;
break;
case 'L': case 'L':
timestamp=1; timestamp=1;
break; break;
case 'M':
devmtu=atoi(optarg);
if(devmtu < MIN_DEVMTU) {
devmtu = MIN_DEVMTU;
}
case 'P': case 'P':
showprogress=1; showprogress=1;
break; break;
@ -782,8 +844,10 @@ fprintf(stderr," -B baudrate 9600,19200,38400,57600,115200 (default),230400\n
#endif #endif
fprintf(stderr," -H Hardware CTS/RTS flow control (default disabled)\n"); fprintf(stderr," -H Hardware CTS/RTS flow control (default disabled)\n");
fprintf(stderr," -I Inquire IP address\n"); fprintf(stderr," -I Inquire IP address\n");
fprintf(stderr," -X Software XON/XOFF flow control (default disabled)\n");
fprintf(stderr," -L Log output format (adds time stamps)\n"); fprintf(stderr," -L Log output format (adds time stamps)\n");
fprintf(stderr," -s siodev Serial device (default /dev/ttyUSB0)\n"); fprintf(stderr," -s siodev Serial device (default /dev/ttyUSB0)\n");
fprintf(stderr," -M Interface MTU (default and min: 1280)\n");
fprintf(stderr," -T Make tap interface (default is tun interface)\n"); fprintf(stderr," -T Make tap interface (default is tun interface)\n");
fprintf(stderr," -t tundev Name of interface (default tap0 or tun0)\n"); fprintf(stderr," -t tundev Name of interface (default tap0 or tun0)\n");
fprintf(stderr," -v[level] Verbosity level\n"); fprintf(stderr," -v[level] Verbosity level\n");
@ -836,9 +900,18 @@ exit(1);
case 460800: case 460800:
b_rate = B460800; b_rate = B460800;
break; break;
case 500000:
b_rate = B500000;
break;
case 576000:
b_rate = B576000;
break;
case 921600: case 921600:
b_rate = B921600; b_rate = B921600;
break; break;
case 1000000:
b_rate = B1000000;
break;
#endif #endif
default: default:
err(1, "unknown baudrate %d", baudrate); err(1, "unknown baudrate %d", baudrate);