2010-04-02 18:17:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001, Adam Dunkels.
|
2010-05-07 12:22:41 +00:00
|
|
|
* Copyright (c) 2009, 2010 Joakim Eriksson, Niclas Finne, Dogan Yazar.
|
2010-04-02 18:17:20 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the uIP TCP/IP stack.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-05-18 16:48:02 +00:00
|
|
|
/* Below define allows importing saved output into Wireshark as "Raw IP" packet type */
|
|
|
|
#define WIRESHARK_IMPORT_FORMAT 1
|
2014-05-10 21:03:11 +00:00
|
|
|
|
2010-04-02 18:17:20 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2017-11-17 21:32:08 +00:00
|
|
|
#include <sys/time.h>
|
2010-04-02 18:17:20 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2010-05-07 12:22:41 +00:00
|
|
|
#include <netdb.h>
|
2010-04-02 18:17:20 +00:00
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
|
2015-08-20 09:40:22 +00:00
|
|
|
#include "tools-utils.h"
|
2015-08-20 08:40:23 +00:00
|
|
|
|
|
|
|
#ifndef BAUDRATE
|
|
|
|
#define BAUDRATE B115200
|
|
|
|
#endif
|
|
|
|
speed_t b_rate = BAUDRATE;
|
|
|
|
|
2010-07-30 20:44:24 +00:00
|
|
|
int verbose = 1;
|
2010-05-25 19:05:31 +00:00
|
|
|
const char *ipaddr;
|
|
|
|
const char *netmask;
|
|
|
|
int slipfd = 0;
|
2010-07-30 20:44:24 +00:00
|
|
|
uint16_t basedelay=0,delaymsec=0;
|
|
|
|
uint32_t startsec,startmsec,delaystartsec,delaystartmsec;
|
2015-08-18 12:57:08 +00:00
|
|
|
int timestamp = 0, flowcontrol=0, showprogress=0, flowcontrol_xonxoff=0;
|
2010-04-02 18:17:20 +00:00
|
|
|
|
|
|
|
int ssystem(const char *fmt, ...)
|
|
|
|
__attribute__((__format__ (__printf__, 1, 2)));
|
|
|
|
void write_to_serial(int outfd, void *inbuf, int len);
|
|
|
|
|
2010-05-25 19:05:31 +00:00
|
|
|
void slip_send(int fd, unsigned char c);
|
2010-11-29 18:14:54 +00:00
|
|
|
void slip_send_char(int fd, unsigned char c);
|
2010-05-25 19:05:31 +00:00
|
|
|
|
2014-05-10 21:15:23 +00:00
|
|
|
#define PROGRESS(s) if(showprogress) fprintf(stderr, s)
|
2010-04-02 18:17:20 +00:00
|
|
|
|
2014-06-27 23:48:41 +00:00
|
|
|
char tundev[1024] = { "" };
|
2010-04-02 18:17:20 +00:00
|
|
|
|
2015-08-18 12:57:08 +00:00
|
|
|
/* IPv6 required minimum MTU */
|
|
|
|
#define MIN_DEVMTU 1500
|
|
|
|
int devmtu = MIN_DEVMTU;
|
|
|
|
|
2010-04-02 18:17:20 +00:00
|
|
|
int
|
|
|
|
ssystem(const char *fmt, ...) __attribute__((__format__ (__printf__, 1, 2)));
|
|
|
|
|
|
|
|
int
|
|
|
|
ssystem(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char cmd[128];
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(cmd, sizeof(cmd), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
printf("%s\n", cmd);
|
|
|
|
fflush(stdout);
|
|
|
|
return system(cmd);
|
|
|
|
}
|
|
|
|
|
2015-08-18 12:57:08 +00:00
|
|
|
#define SLIP_END 0300
|
|
|
|
#define SLIP_ESC 0333
|
|
|
|
#define SLIP_ESC_END 0334
|
|
|
|
#define SLIP_ESC_ESC 0335
|
2010-04-02 18:17:20 +00:00
|
|
|
|
2015-08-18 12:57:08 +00:00
|
|
|
#define SLIP_ESC_XON 0336
|
|
|
|
#define SLIP_ESC_XOFF 0337
|
|
|
|
#define XON 17
|
|
|
|
#define XOFF 19
|
2010-04-02 18:17:20 +00:00
|
|
|
|
2010-05-07 12:22:41 +00:00
|
|
|
/* get sockaddr, IPv4 or IPv6: */
|
|
|
|
void *
|
|
|
|
get_in_addr(struct sockaddr *sa)
|
|
|
|
{
|
|
|
|
if(sa->sa_family == AF_INET) {
|
|
|
|
return &(((struct sockaddr_in*)sa)->sin_addr);
|
|
|
|
}
|
|
|
|
return &(((struct sockaddr_in6*)sa)->sin6_addr);
|
|
|
|
}
|
2010-07-30 20:44:24 +00:00
|
|
|
void
|
|
|
|
stamptime(void)
|
|
|
|
{
|
|
|
|
static long startsecs=0,startmsecs=0;
|
|
|
|
long secs,msecs;
|
|
|
|
struct timeval tv;
|
|
|
|
time_t t;
|
|
|
|
struct tm *tmp;
|
|
|
|
char timec[20];
|
2014-05-10 21:03:11 +00:00
|
|
|
|
2010-07-30 20:44:24 +00:00
|
|
|
gettimeofday(&tv, NULL) ;
|
|
|
|
msecs=tv.tv_usec/1000;
|
|
|
|
secs=tv.tv_sec;
|
|
|
|
if (startsecs) {
|
|
|
|
secs -=startsecs;
|
|
|
|
msecs-=startmsecs;
|
|
|
|
if (msecs<0) {secs--;msecs+=1000;}
|
|
|
|
fprintf(stderr,"%04lu.%03lu ", secs, msecs);
|
|
|
|
} else {
|
|
|
|
startsecs=secs;
|
|
|
|
startmsecs=msecs;
|
|
|
|
t=time(NULL);
|
|
|
|
tmp=localtime(&t);
|
|
|
|
strftime(timec,sizeof(timec),"%T",tmp);
|
|
|
|
// fprintf(stderr,"\n%s.%03lu ",timec,msecs);
|
|
|
|
fprintf(stderr,"\n%s ",timec);
|
|
|
|
}
|
|
|
|
}
|
2010-05-07 12:22:41 +00:00
|
|
|
|
2010-04-02 18:17:20 +00:00
|
|
|
int
|
|
|
|
is_sensible_string(const unsigned char *s, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 1; i < len; i++) {
|
|
|
|
if(s[i] == 0 || s[i] == '\r' || s[i] == '\n' || s[i] == '\t') {
|
|
|
|
continue;
|
|
|
|
} else if(s[i] < ' ' || '~' < s[i]) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read from serial, when we have a packet write it to tun. No output
|
|
|
|
* buffering, input buffered by stdio.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
serial_to_tun(FILE *inslip, int outfd)
|
|
|
|
{
|
|
|
|
static union {
|
|
|
|
unsigned char inbuf[2000];
|
|
|
|
} uip;
|
|
|
|
static int inbufptr = 0;
|
2010-07-30 20:44:24 +00:00
|
|
|
int ret,i;
|
2010-04-02 18:17:20 +00:00
|
|
|
unsigned char c;
|
|
|
|
|
|
|
|
#ifdef linux
|
|
|
|
ret = fread(&c, 1, 1, inslip);
|
|
|
|
if(ret == -1 || ret == 0) err(1, "serial_to_tun: read");
|
|
|
|
goto after_fread;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
read_more:
|
|
|
|
if(inbufptr >= sizeof(uip.inbuf)) {
|
2010-07-30 20:44:24 +00:00
|
|
|
if(timestamp) stamptime();
|
2011-04-08 19:15:16 +00:00
|
|
|
fprintf(stderr, "*** dropping large %d byte packet\n",inbufptr);
|
2011-08-16 18:25:16 +00:00
|
|
|
inbufptr = 0;
|
2010-04-02 18:17:20 +00:00
|
|
|
}
|
|
|
|
ret = fread(&c, 1, 1, inslip);
|
|
|
|
#ifdef linux
|
|
|
|
after_fread:
|
|
|
|
#endif
|
|
|
|
if(ret == -1) {
|
|
|
|
err(1, "serial_to_tun: read");
|
|
|
|
}
|
|
|
|
if(ret == 0) {
|
|
|
|
clearerr(inslip);
|
|
|
|
return;
|
|
|
|
}
|
2014-05-10 21:15:23 +00:00
|
|
|
PROGRESS(".");
|
2010-04-02 18:17:20 +00:00
|
|
|
switch(c) {
|
|
|
|
case SLIP_END:
|
|
|
|
if(inbufptr > 0) {
|
|
|
|
if(uip.inbuf[0] == '!') {
|
2010-05-07 12:22:41 +00:00
|
|
|
if(uip.inbuf[1] == 'M') {
|
2010-04-02 18:17:20 +00:00
|
|
|
/* Read gateway MAC address and autoconfigure tap0 interface */
|
|
|
|
char macs[24];
|
|
|
|
int i, pos;
|
|
|
|
for(i = 0, pos = 0; i < 16; i++) {
|
|
|
|
macs[pos++] = uip.inbuf[2 + i];
|
2010-05-07 12:22:41 +00:00
|
|
|
if((i & 1) == 1 && i < 14) {
|
2010-04-02 18:17:20 +00:00
|
|
|
macs[pos++] = ':';
|
|
|
|
}
|
|
|
|
}
|
2010-07-30 20:44:24 +00:00
|
|
|
if(timestamp) stamptime();
|
2010-04-02 18:17:20 +00:00
|
|
|
macs[pos] = '\0';
|
2010-07-30 20:44:24 +00:00
|
|
|
// printf("*** Gateway's MAC address: %s\n", macs);
|
|
|
|
fprintf(stderr,"*** Gateway's MAC address: %s\n", macs);
|
|
|
|
if (timestamp) stamptime();
|
2010-04-02 18:17:20 +00:00
|
|
|
ssystem("ifconfig %s down", tundev);
|
2010-07-30 20:44:24 +00:00
|
|
|
if (timestamp) stamptime();
|
2010-04-02 18:17:20 +00:00
|
|
|
ssystem("ifconfig %s hw ether %s", tundev, &macs[6]);
|
2010-07-30 20:44:24 +00:00
|
|
|
if (timestamp) stamptime();
|
2010-04-02 18:17:20 +00:00
|
|
|
ssystem("ifconfig %s up", tundev);
|
2011-03-11 15:34:30 +00:00
|
|
|
}
|
|
|
|
} else if(uip.inbuf[0] == '?') {
|
|
|
|
if(uip.inbuf[1] == 'P') {
|
2010-05-25 19:05:31 +00:00
|
|
|
/* Prefix info requested */
|
|
|
|
struct in6_addr addr;
|
|
|
|
int i;
|
|
|
|
char *s = strchr(ipaddr, '/');
|
|
|
|
if(s != NULL) {
|
|
|
|
*s = '\0';
|
|
|
|
}
|
|
|
|
inet_pton(AF_INET6, ipaddr, &addr);
|
2010-07-30 20:44:24 +00:00
|
|
|
if(timestamp) stamptime();
|
|
|
|
fprintf(stderr,"*** Address:%s => %02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
|
2014-05-10 21:03:11 +00:00
|
|
|
ipaddr,
|
2010-05-25 19:05:31 +00:00
|
|
|
addr.s6_addr[0], addr.s6_addr[1],
|
|
|
|
addr.s6_addr[2], addr.s6_addr[3],
|
|
|
|
addr.s6_addr[4], addr.s6_addr[5],
|
|
|
|
addr.s6_addr[6], addr.s6_addr[7]);
|
|
|
|
slip_send(slipfd, '!');
|
|
|
|
slip_send(slipfd, 'P');
|
|
|
|
for(i = 0; i < 8; i++) {
|
2010-11-29 18:14:54 +00:00
|
|
|
/* need to call the slip_send_char for stuffing */
|
|
|
|
slip_send_char(slipfd, addr.s6_addr[i]);
|
2010-05-25 19:05:31 +00:00
|
|
|
}
|
|
|
|
slip_send(slipfd, SLIP_END);
|
|
|
|
}
|
2010-04-02 18:17:20 +00:00
|
|
|
#define DEBUG_LINE_MARKER '\r'
|
2015-10-13 07:35:25 +00:00
|
|
|
} else if(uip.inbuf[0] == DEBUG_LINE_MARKER) {
|
|
|
|
fwrite(uip.inbuf + 1, inbufptr - 1, 1, stdout);
|
|
|
|
} else if(is_sensible_string(uip.inbuf, inbufptr)) {
|
|
|
|
if(verbose==1) { /* strings already echoed below for verbose>1 */
|
|
|
|
if (timestamp) stamptime();
|
|
|
|
fwrite(uip.inbuf, inbufptr, 1, stdout);
|
|
|
|
}
|
2010-04-02 18:17:20 +00:00
|
|
|
} else {
|
2010-07-30 20:44:24 +00:00
|
|
|
if(verbose>2) {
|
|
|
|
if (timestamp) stamptime();
|
|
|
|
printf("Packet from SLIP of length %d - write TUN\n", inbufptr);
|
|
|
|
if (verbose>4) {
|
2011-05-18 16:48:02 +00:00
|
|
|
#if WIRESHARK_IMPORT_FORMAT
|
|
|
|
printf("0000");
|
|
|
|
for(i = 0; i < inbufptr; i++) printf(" %02x",uip.inbuf[i]);
|
|
|
|
#else
|
2010-07-30 20:44:24 +00:00
|
|
|
printf(" ");
|
|
|
|
for(i = 0; i < inbufptr; i++) {
|
|
|
|
printf("%02x", uip.inbuf[i]);
|
2011-05-18 16:48:02 +00:00
|
|
|
if((i & 3) == 3) printf(" ");
|
|
|
|
if((i & 15) == 15) printf("\n ");
|
2010-07-30 20:44:24 +00:00
|
|
|
}
|
2011-05-18 16:48:02 +00:00
|
|
|
#endif
|
2010-07-30 20:44:24 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 18:17:20 +00:00
|
|
|
if(write(outfd, uip.inbuf, inbufptr) != inbufptr) {
|
|
|
|
err(1, "serial_to_tun: write");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inbufptr = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SLIP_ESC:
|
|
|
|
if(fread(&c, 1, 1, inslip) != 1) {
|
|
|
|
clearerr(inslip);
|
|
|
|
/* Put ESC back and give up! */
|
|
|
|
ungetc(SLIP_ESC, inslip);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(c) {
|
|
|
|
case SLIP_ESC_END:
|
|
|
|
c = SLIP_END;
|
|
|
|
break;
|
|
|
|
case SLIP_ESC_ESC:
|
|
|
|
c = SLIP_ESC;
|
|
|
|
break;
|
2015-08-18 12:57:08 +00:00
|
|
|
case SLIP_ESC_XON:
|
|
|
|
c = XON;
|
|
|
|
break;
|
|
|
|
case SLIP_ESC_XOFF:
|
|
|
|
c = XOFF;
|
|
|
|
break;
|
2010-04-02 18:17:20 +00:00
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
|
|
|
uip.inbuf[inbufptr++] = c;
|
2010-07-30 20:44:24 +00:00
|
|
|
|
|
|
|
/* Echo lines as they are received for verbose=2,3,5+ */
|
|
|
|
/* Echo all printable characters for verbose==4 */
|
|
|
|
if((verbose==2) || (verbose==3) || (verbose>4)) {
|
|
|
|
if(c=='\n') {
|
|
|
|
if(is_sensible_string(uip.inbuf, inbufptr)) {
|
|
|
|
if (timestamp) stamptime();
|
|
|
|
fwrite(uip.inbuf, inbufptr, 1, stdout);
|
|
|
|
inbufptr=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(verbose==4) {
|
|
|
|
if(c == 0 || c == '\r' || c == '\n' || c == '\t' || (c >= ' ' && c <= '~')) {
|
|
|
|
fwrite(&c, 1, 1, stdout);
|
|
|
|
if(c=='\n') if(timestamp) stamptime();
|
|
|
|
}
|
|
|
|
}
|
2014-05-10 21:03:11 +00:00
|
|
|
|
2010-04-02 18:17:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto read_more;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char slip_buf[2000];
|
|
|
|
int slip_end, slip_begin;
|
|
|
|
|
2010-11-29 18:14:54 +00:00
|
|
|
void
|
|
|
|
slip_send_char(int fd, unsigned char c)
|
|
|
|
{
|
|
|
|
switch(c) {
|
|
|
|
case SLIP_END:
|
|
|
|
slip_send(fd, SLIP_ESC);
|
|
|
|
slip_send(fd, SLIP_ESC_END);
|
|
|
|
break;
|
|
|
|
case SLIP_ESC:
|
|
|
|
slip_send(fd, SLIP_ESC);
|
|
|
|
slip_send(fd, SLIP_ESC_ESC);
|
|
|
|
break;
|
2015-08-18 12:57:08 +00:00
|
|
|
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;
|
2010-11-29 18:14:54 +00:00
|
|
|
default:
|
|
|
|
slip_send(fd, c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 18:17:20 +00:00
|
|
|
void
|
|
|
|
slip_send(int fd, unsigned char c)
|
|
|
|
{
|
2010-05-07 12:22:41 +00:00
|
|
|
if(slip_end >= sizeof(slip_buf)) {
|
2010-04-02 18:17:20 +00:00
|
|
|
err(1, "slip_send overflow");
|
2010-05-07 12:22:41 +00:00
|
|
|
}
|
2010-04-02 18:17:20 +00:00
|
|
|
slip_buf[slip_end] = c;
|
|
|
|
slip_end++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
slip_empty()
|
|
|
|
{
|
|
|
|
return slip_end == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
slip_flushbuf(int fd)
|
|
|
|
{
|
|
|
|
int n;
|
2014-05-10 21:03:11 +00:00
|
|
|
|
2010-05-07 12:22:41 +00:00
|
|
|
if(slip_empty()) {
|
2010-04-02 18:17:20 +00:00
|
|
|
return;
|
2010-05-07 12:22:41 +00:00
|
|
|
}
|
|
|
|
|
2010-04-02 18:17:20 +00:00
|
|
|
n = write(fd, slip_buf + slip_begin, (slip_end - slip_begin));
|
|
|
|
|
|
|
|
if(n == -1 && errno != EAGAIN) {
|
|
|
|
err(1, "slip_flushbuf write failed");
|
|
|
|
} else if(n == -1) {
|
|
|
|
PROGRESS("Q"); /* Outqueueis full! */
|
|
|
|
} else {
|
|
|
|
slip_begin += n;
|
|
|
|
if(slip_begin == slip_end) {
|
|
|
|
slip_begin = slip_end = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
write_to_serial(int outfd, void *inbuf, int len)
|
|
|
|
{
|
|
|
|
u_int8_t *p = inbuf;
|
2010-05-07 12:22:41 +00:00
|
|
|
int i;
|
2010-04-02 18:17:20 +00:00
|
|
|
|
2010-07-30 20:44:24 +00:00
|
|
|
if(verbose>2) {
|
|
|
|
if (timestamp) stamptime();
|
|
|
|
printf("Packet from TUN of length %d - write SLIP\n", len);
|
|
|
|
if (verbose>4) {
|
2011-05-18 16:48:02 +00:00
|
|
|
#if WIRESHARK_IMPORT_FORMAT
|
|
|
|
printf("0000");
|
|
|
|
for(i = 0; i < len; i++) printf(" %02x", p[i]);
|
|
|
|
#else
|
2010-07-30 20:44:24 +00:00
|
|
|
printf(" ");
|
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
printf("%02x", p[i]);
|
2011-05-18 16:48:02 +00:00
|
|
|
if((i & 3) == 3) printf(" ");
|
|
|
|
if((i & 15) == 15) printf("\n ");
|
2010-05-07 12:22:41 +00:00
|
|
|
}
|
2011-05-18 16:48:02 +00:00
|
|
|
#endif
|
2010-04-02 18:17:20 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It would be ``nice'' to send a SLIP_END here but it's not
|
|
|
|
* really necessary.
|
|
|
|
*/
|
|
|
|
/* slip_send(outfd, SLIP_END); */
|
2010-07-30 20:44:24 +00:00
|
|
|
|
2010-04-02 18:17:20 +00:00
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
switch(p[i]) {
|
|
|
|
case SLIP_END:
|
|
|
|
slip_send(outfd, SLIP_ESC);
|
|
|
|
slip_send(outfd, SLIP_ESC_END);
|
|
|
|
break;
|
|
|
|
case SLIP_ESC:
|
|
|
|
slip_send(outfd, SLIP_ESC);
|
|
|
|
slip_send(outfd, SLIP_ESC_ESC);
|
|
|
|
break;
|
2015-08-18 12:57:08 +00:00
|
|
|
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;
|
2010-04-02 18:17:20 +00:00
|
|
|
default:
|
|
|
|
slip_send(outfd, p[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
slip_send(outfd, SLIP_END);
|
|
|
|
PROGRESS("t");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read from tun, write to slip.
|
|
|
|
*/
|
2010-07-30 20:44:24 +00:00
|
|
|
int
|
2010-04-02 18:17:20 +00:00
|
|
|
tun_to_serial(int infd, int outfd)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
unsigned char inbuf[2000];
|
|
|
|
} uip;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if((size = read(infd, uip.inbuf, 2000)) == -1) err(1, "tun_to_serial: read");
|
|
|
|
|
|
|
|
write_to_serial(outfd, uip.inbuf, size);
|
2010-07-30 20:44:24 +00:00
|
|
|
return size;
|
2010-04-02 18:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
stty_telos(int fd)
|
|
|
|
{
|
|
|
|
struct termios tty;
|
|
|
|
speed_t speed = b_rate;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if(tcflush(fd, TCIOFLUSH) == -1) err(1, "tcflush");
|
|
|
|
|
|
|
|
if(tcgetattr(fd, &tty) == -1) err(1, "tcgetattr");
|
|
|
|
|
|
|
|
cfmakeraw(&tty);
|
|
|
|
|
|
|
|
/* Nonblocking read. */
|
|
|
|
tty.c_cc[VTIME] = 0;
|
|
|
|
tty.c_cc[VMIN] = 0;
|
2011-04-08 19:15:16 +00:00
|
|
|
if (flowcontrol)
|
|
|
|
tty.c_cflag |= CRTSCTS;
|
|
|
|
else
|
|
|
|
tty.c_cflag &= ~CRTSCTS;
|
2015-08-18 12:57:08 +00:00
|
|
|
tty.c_iflag &= ~IXON;
|
|
|
|
if(flowcontrol_xonxoff) {
|
|
|
|
tty.c_iflag |= IXOFF | IXANY;
|
|
|
|
} else {
|
|
|
|
tty.c_iflag &= ~IXOFF & ~IXANY;
|
|
|
|
}
|
2010-04-02 18:17:20 +00:00
|
|
|
tty.c_cflag &= ~HUPCL;
|
|
|
|
tty.c_cflag &= ~CLOCAL;
|
|
|
|
|
|
|
|
cfsetispeed(&tty, speed);
|
|
|
|
cfsetospeed(&tty, speed);
|
|
|
|
|
|
|
|
if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) err(1, "tcsetattr");
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
/* Nonblocking read and write. */
|
|
|
|
/* if(fcntl(fd, F_SETFL, O_NONBLOCK) == -1) err(1, "fcntl"); */
|
|
|
|
|
|
|
|
tty.c_cflag |= CLOCAL;
|
|
|
|
if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) err(1, "tcsetattr");
|
|
|
|
|
|
|
|
i = TIOCM_DTR;
|
|
|
|
if(ioctl(fd, TIOCMBIS, &i) == -1) err(1, "ioctl");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
usleep(10*1000); /* Wait for hardware 10ms. */
|
|
|
|
|
|
|
|
/* Flush input and output buffers. */
|
|
|
|
if(tcflush(fd, TCIOFLUSH) == -1) err(1, "tcflush");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
devopen(const char *dev, int flags)
|
|
|
|
{
|
2014-06-27 23:48:41 +00:00
|
|
|
char t[1024];
|
2010-04-02 18:17:20 +00:00
|
|
|
strcpy(t, "/dev/");
|
2010-05-07 12:22:41 +00:00
|
|
|
strncat(t, dev, sizeof(t) - 5);
|
2010-04-02 18:17:20 +00:00
|
|
|
return open(t, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef linux
|
|
|
|
#include <linux/if.h>
|
|
|
|
#include <linux/if_tun.h>
|
|
|
|
|
|
|
|
int
|
2010-04-16 12:39:46 +00:00
|
|
|
tun_alloc(char *dev, int tap)
|
2010-04-02 18:17:20 +00:00
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd, err;
|
|
|
|
|
2010-05-07 12:22:41 +00:00
|
|
|
if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
|
2014-05-09 18:25:59 +00:00
|
|
|
perror("can not open /dev/net/tun");
|
2010-04-02 18:17:20 +00:00
|
|
|
return -1;
|
2010-05-07 12:22:41 +00:00
|
|
|
}
|
2010-04-02 18:17:20 +00:00
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
|
|
|
|
/* Flags: IFF_TUN - TUN device (no Ethernet headers)
|
|
|
|
* IFF_TAP - TAP device
|
|
|
|
*
|
|
|
|
* IFF_NO_PI - Do not provide packet information
|
|
|
|
*/
|
2010-04-16 12:39:46 +00:00
|
|
|
ifr.ifr_flags = (tap ? IFF_TAP : IFF_TUN) | IFF_NO_PI;
|
2018-08-13 15:46:08 +00:00
|
|
|
if(*dev != 0) {
|
|
|
|
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name) - 1);
|
|
|
|
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
|
|
|
|
}
|
2010-04-02 18:17:20 +00:00
|
|
|
|
2010-05-07 12:22:41 +00:00
|
|
|
if((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) {
|
2010-04-02 18:17:20 +00:00
|
|
|
close(fd);
|
2014-05-09 18:25:59 +00:00
|
|
|
fprintf(stderr, "can not tunsetiff to %s (flags=%08x): %s\n", dev, ifr.ifr_flags,
|
|
|
|
strerror(errno));
|
2010-04-02 18:17:20 +00:00
|
|
|
return err;
|
|
|
|
}
|
2014-05-09 18:25:59 +00:00
|
|
|
|
|
|
|
/* get resulting tunnel name */
|
2010-04-02 18:17:20 +00:00
|
|
|
strcpy(dev, ifr.ifr_name);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int
|
2010-04-16 12:39:46 +00:00
|
|
|
tun_alloc(char *dev, int tap)
|
2010-04-02 18:17:20 +00:00
|
|
|
{
|
|
|
|
return devopen(dev, O_RDWR);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
cleanup(void)
|
|
|
|
{
|
2012-04-04 08:17:29 +00:00
|
|
|
#ifndef __APPLE__
|
2010-07-30 20:44:24 +00:00
|
|
|
if (timestamp) stamptime();
|
2010-04-02 18:17:20 +00:00
|
|
|
ssystem("ifconfig %s down", tundev);
|
|
|
|
#ifndef linux
|
|
|
|
ssystem("sysctl -w net.ipv6.conf.all.forwarding=1");
|
|
|
|
#endif
|
|
|
|
/* ssystem("arp -d %s", ipaddr); */
|
2010-07-30 20:44:24 +00:00
|
|
|
if (timestamp) stamptime();
|
2010-04-02 18:17:20 +00:00
|
|
|
ssystem("netstat -nr"
|
|
|
|
" | awk '{ if ($2 == \"%s\") print \"route delete -net \"$1; }'"
|
|
|
|
" | sh",
|
|
|
|
tundev);
|
2012-04-04 08:17:29 +00:00
|
|
|
#else
|
|
|
|
{
|
|
|
|
char * itfaddr = strdup(ipaddr);
|
|
|
|
char * prefix = index(itfaddr, '/');
|
|
|
|
if (timestamp) stamptime();
|
|
|
|
ssystem("ifconfig %s inet6 %s remove", tundev, ipaddr);
|
|
|
|
if (timestamp) stamptime();
|
|
|
|
ssystem("ifconfig %s down", tundev);
|
|
|
|
if ( prefix != NULL ) *prefix = '\0';
|
|
|
|
ssystem("route delete -inet6 %s", itfaddr);
|
|
|
|
free(itfaddr);
|
|
|
|
}
|
|
|
|
#endif
|
2010-04-02 18:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sigcleanup(int signo)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "signal %d\n", signo);
|
|
|
|
exit(0); /* exit(0) will call cleanup() */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int got_sigalarm;
|
|
|
|
|
|
|
|
void
|
|
|
|
sigalarm(int signo)
|
|
|
|
{
|
|
|
|
got_sigalarm = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sigalarm_reset()
|
|
|
|
{
|
|
|
|
#ifdef linux
|
|
|
|
#define TIMEOUT (997*1000)
|
|
|
|
#else
|
|
|
|
#define TIMEOUT (2451*1000)
|
|
|
|
#endif
|
|
|
|
ualarm(TIMEOUT, TIMEOUT);
|
|
|
|
got_sigalarm = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ifconf(const char *tundev, const char *ipaddr)
|
|
|
|
{
|
|
|
|
#ifdef linux
|
2010-07-30 20:44:24 +00:00
|
|
|
if (timestamp) stamptime();
|
2015-08-18 12:57:08 +00:00
|
|
|
ssystem("ifconfig %s inet `hostname` mtu %d up", tundev, devmtu);
|
2010-07-30 20:44:24 +00:00
|
|
|
if (timestamp) stamptime();
|
2010-04-02 18:17:20 +00:00
|
|
|
ssystem("ifconfig %s add %s", tundev, ipaddr);
|
2012-03-02 20:28:26 +00:00
|
|
|
|
|
|
|
/* radvd needs a link local address for routing */
|
|
|
|
#if 0
|
|
|
|
/* fe80::1/64 is good enough */
|
|
|
|
ssystem("ifconfig %s add fe80::1/64", tundev);
|
|
|
|
#elif 1
|
|
|
|
/* Generate a link local address a la sixxs/aiccu */
|
|
|
|
/* First a full parse, stripping off the prefix length */
|
2012-03-01 00:45:05 +00:00
|
|
|
{
|
|
|
|
char lladdr[40];
|
|
|
|
char c, *ptr=(char *)ipaddr;
|
|
|
|
uint16_t digit,ai,a[8],cc,scc,i;
|
|
|
|
for(ai=0; ai<8; ai++) {
|
|
|
|
a[ai]=0;
|
2012-03-02 20:28:26 +00:00
|
|
|
}
|
2012-03-01 00:45:05 +00:00
|
|
|
ai=0;
|
|
|
|
cc=scc=0;
|
2017-11-17 21:32:08 +00:00
|
|
|
while((c=*ptr++) != 0) {
|
2012-03-01 00:45:05 +00:00
|
|
|
if(c=='/') break;
|
|
|
|
if(c==':') {
|
|
|
|
if(cc)
|
|
|
|
scc = ai;
|
|
|
|
cc = 1;
|
|
|
|
if(++ai>7) break;
|
|
|
|
} else {
|
|
|
|
cc=0;
|
|
|
|
digit = c-'0';
|
2014-05-10 21:03:11 +00:00
|
|
|
if (digit > 9)
|
2012-03-01 00:45:05 +00:00
|
|
|
digit = 10 + (c & 0xdf) - 'A';
|
|
|
|
a[ai] = (a[ai] << 4) + digit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Get # elided and shift what's after to the end */
|
|
|
|
cc=8-ai;
|
|
|
|
for(i=0;i<cc;i++) {
|
|
|
|
if ((8-i-cc) <= scc) {
|
|
|
|
a[7-i] = 0;
|
|
|
|
} else {
|
|
|
|
a[7-i] = a[8-i-cc];
|
|
|
|
a[8-i-cc]=0;
|
|
|
|
}
|
2012-03-02 20:28:26 +00:00
|
|
|
}
|
2012-03-01 00:45:05 +00:00
|
|
|
sprintf(lladdr,"fe80::%x:%x:%x:%x",a[1]&0xfefd,a[2],a[3],a[7]);
|
|
|
|
if (timestamp) stamptime();
|
|
|
|
ssystem("ifconfig %s add %s/64", tundev, lladdr);
|
2012-03-02 20:28:26 +00:00
|
|
|
}
|
|
|
|
#endif /* link local */
|
2012-04-04 08:17:29 +00:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
{
|
|
|
|
char * itfaddr = strdup(ipaddr);
|
|
|
|
char * prefix = index(itfaddr, '/');
|
|
|
|
if ( prefix != NULL ) {
|
|
|
|
*prefix = '\0';
|
|
|
|
prefix++;
|
|
|
|
} else {
|
|
|
|
prefix = "64";
|
|
|
|
}
|
|
|
|
if (timestamp) stamptime();
|
2015-08-18 12:57:08 +00:00
|
|
|
ssystem("ifconfig %s inet6 mtu %d up", tundev, devmtu);
|
2012-04-04 08:17:29 +00:00
|
|
|
if (timestamp) stamptime();
|
|
|
|
ssystem("ifconfig %s inet6 %s add", tundev, ipaddr );
|
|
|
|
if (timestamp) stamptime();
|
|
|
|
ssystem("sysctl -w net.inet6.ip6.forwarding=1");
|
|
|
|
free(itfaddr);
|
|
|
|
}
|
2010-04-02 18:17:20 +00:00
|
|
|
#else
|
2010-07-30 20:44:24 +00:00
|
|
|
if (timestamp) stamptime();
|
2015-08-18 12:57:08 +00:00
|
|
|
ssystem("ifconfig %s inet `hostname` %s mtu %d up", tundev, ipaddr, devmtu);
|
2010-07-30 20:44:24 +00:00
|
|
|
if (timestamp) stamptime();
|
2010-04-02 18:17:20 +00:00
|
|
|
ssystem("sysctl -w net.inet.ip.forwarding=1");
|
|
|
|
#endif /* !linux */
|
|
|
|
|
2010-07-30 20:44:24 +00:00
|
|
|
if (timestamp) stamptime();
|
2010-04-02 18:17:20 +00:00
|
|
|
ssystem("ifconfig %s\n", tundev);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c;
|
2010-05-25 19:05:31 +00:00
|
|
|
int tunfd, maxfd;
|
2010-04-02 18:17:20 +00:00
|
|
|
int ret;
|
|
|
|
fd_set rset, wset;
|
|
|
|
FILE *inslip;
|
|
|
|
const char *siodev = NULL;
|
2010-05-07 12:22:41 +00:00
|
|
|
const char *host = NULL;
|
|
|
|
const char *port = NULL;
|
|
|
|
const char *prog;
|
2010-04-02 18:17:20 +00:00
|
|
|
int baudrate = -2;
|
2015-10-13 07:54:06 +00:00
|
|
|
int ipa_enable = 0;
|
2010-04-16 12:39:46 +00:00
|
|
|
int tap = 0;
|
2010-05-07 12:22:41 +00:00
|
|
|
slipfd = 0;
|
2010-04-02 18:17:20 +00:00
|
|
|
|
2010-05-07 12:22:41 +00:00
|
|
|
prog = argv[0];
|
2010-04-02 18:17:20 +00:00
|
|
|
setvbuf(stdout, NULL, _IOLBF, 0); /* Line buffered output. */
|
|
|
|
|
2015-08-18 12:57:08 +00:00
|
|
|
while((c = getopt(argc, argv, "B:HILPhXM:s:t:v::d::a:p:T")) != -1) {
|
2010-05-07 12:22:41 +00:00
|
|
|
switch(c) {
|
2010-04-02 18:17:20 +00:00
|
|
|
case 'B':
|
|
|
|
baudrate = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
2011-04-08 19:15:16 +00:00
|
|
|
case 'H':
|
|
|
|
flowcontrol=1;
|
|
|
|
break;
|
2014-05-10 21:03:11 +00:00
|
|
|
|
2015-08-18 12:57:08 +00:00
|
|
|
case 'X':
|
|
|
|
flowcontrol_xonxoff=1;
|
|
|
|
break;
|
|
|
|
|
2010-07-30 20:44:24 +00:00
|
|
|
case 'L':
|
|
|
|
timestamp=1;
|
|
|
|
break;
|
|
|
|
|
2015-08-18 12:57:08 +00:00
|
|
|
case 'M':
|
|
|
|
devmtu=atoi(optarg);
|
|
|
|
if(devmtu < MIN_DEVMTU) {
|
|
|
|
devmtu = MIN_DEVMTU;
|
|
|
|
}
|
|
|
|
|
2014-05-10 21:15:23 +00:00
|
|
|
case 'P':
|
|
|
|
showprogress=1;
|
|
|
|
break;
|
|
|
|
|
2010-04-02 18:17:20 +00:00
|
|
|
case 's':
|
|
|
|
if(strncmp("/dev/", optarg, 5) == 0) {
|
2018-08-16 12:00:54 +00:00
|
|
|
siodev = optarg + 5;
|
2010-04-02 18:17:20 +00:00
|
|
|
} else {
|
2018-08-16 12:00:54 +00:00
|
|
|
siodev = optarg;
|
2010-04-02 18:17:20 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-05-10 21:19:51 +00:00
|
|
|
case 'I':
|
2015-10-13 07:54:06 +00:00
|
|
|
ipa_enable = 1;
|
|
|
|
fprintf(stderr, "Will inquire about IP address using IPA=\n");
|
2014-05-10 21:19:51 +00:00
|
|
|
break;
|
|
|
|
|
2010-04-02 18:17:20 +00:00
|
|
|
case 't':
|
|
|
|
if(strncmp("/dev/", optarg, 5) == 0) {
|
2018-08-13 15:46:08 +00:00
|
|
|
strncpy(tundev, optarg + 5, sizeof(tundev) - 1);
|
2010-04-02 18:17:20 +00:00
|
|
|
} else {
|
2018-08-13 15:46:08 +00:00
|
|
|
strncpy(tundev, optarg, sizeof(tundev) - 1);
|
2010-04-02 18:17:20 +00:00
|
|
|
}
|
2018-08-13 15:46:08 +00:00
|
|
|
tundev[sizeof(tundev) - 1] = '\0';
|
2010-04-02 18:17:20 +00:00
|
|
|
break;
|
2010-05-07 12:22:41 +00:00
|
|
|
|
|
|
|
case 'a':
|
|
|
|
host = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
port = optarg;
|
|
|
|
break;
|
|
|
|
|
2010-07-30 20:44:24 +00:00
|
|
|
case 'd':
|
|
|
|
basedelay = 10;
|
|
|
|
if (optarg) basedelay = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
2010-04-02 18:17:20 +00:00
|
|
|
case 'v':
|
2010-07-30 20:44:24 +00:00
|
|
|
verbose = 2;
|
|
|
|
if (optarg) verbose = atoi(optarg);
|
2010-04-02 18:17:20 +00:00
|
|
|
break;
|
2010-07-30 20:44:24 +00:00
|
|
|
|
2010-04-16 12:39:46 +00:00
|
|
|
case 'T':
|
|
|
|
tap = 1;
|
|
|
|
break;
|
2014-05-10 21:03:11 +00:00
|
|
|
|
2010-04-02 18:17:20 +00:00
|
|
|
case '?':
|
|
|
|
case 'h':
|
|
|
|
default:
|
2010-07-30 20:44:24 +00:00
|
|
|
fprintf(stderr,"usage: %s [options] ipaddress\n", prog);
|
2015-09-30 14:06:58 +00:00
|
|
|
fprintf(stderr,"example: tunslip6 -L -v2 -s ttyUSB1 fd00::1/64\n");
|
2010-07-30 20:44:24 +00:00
|
|
|
fprintf(stderr,"Options are:\n");
|
2012-04-04 08:17:29 +00:00
|
|
|
#ifndef __APPLE__
|
|
|
|
fprintf(stderr," -B baudrate 9600,19200,38400,57600,115200 (default),230400,460800,921600\n");
|
|
|
|
#else
|
|
|
|
fprintf(stderr," -B baudrate 9600,19200,38400,57600,115200 (default),230400\n");
|
|
|
|
#endif
|
2011-04-08 19:15:16 +00:00
|
|
|
fprintf(stderr," -H Hardware CTS/RTS flow control (default disabled)\n");
|
2015-10-13 07:54:06 +00:00
|
|
|
fprintf(stderr," -I Inquire IP address\n");
|
2015-08-18 12:57:08 +00:00
|
|
|
fprintf(stderr," -X Software XON/XOFF flow control (default disabled)\n");
|
2010-07-30 20:44:24 +00:00
|
|
|
fprintf(stderr," -L Log output format (adds time stamps)\n");
|
|
|
|
fprintf(stderr," -s siodev Serial device (default /dev/ttyUSB0)\n");
|
2015-08-18 12:57:08 +00:00
|
|
|
fprintf(stderr," -M Interface MTU (default and min: 1280)\n");
|
2010-07-30 20:44:24 +00:00
|
|
|
fprintf(stderr," -T Make tap interface (default is tun interface)\n");
|
|
|
|
fprintf(stderr," -t tundev Name of interface (default tap0 or tun0)\n");
|
2018-11-11 16:25:36 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
fprintf(stderr," -v level Verbosity level\n");
|
|
|
|
#else
|
2010-07-30 20:44:24 +00:00
|
|
|
fprintf(stderr," -v[level] Verbosity level\n");
|
2018-11-11 16:25:36 +00:00
|
|
|
#endif
|
2010-07-30 20:44:24 +00:00
|
|
|
fprintf(stderr," -v0 No messages\n");
|
|
|
|
fprintf(stderr," -v1 Encapsulated SLIP debug messages (default)\n");
|
|
|
|
fprintf(stderr," -v2 Printable strings after they are received\n");
|
|
|
|
fprintf(stderr," -v3 Printable strings and SLIP packet notifications\n");
|
|
|
|
fprintf(stderr," -v4 All printable characters as they are received\n");
|
|
|
|
fprintf(stderr," -v5 All SLIP packets in hex\n");
|
2018-11-11 16:25:36 +00:00
|
|
|
#ifndef __APPLE__
|
2010-07-30 20:44:24 +00:00
|
|
|
fprintf(stderr," -v Equivalent to -v3\n");
|
2018-11-11 16:25:36 +00:00
|
|
|
#endif
|
2010-07-30 20:44:24 +00:00
|
|
|
fprintf(stderr," -d[basedelay] Minimum delay between outgoing SLIP packets.\n");
|
|
|
|
fprintf(stderr," Actual delay is basedelay*(#6LowPAN fragments) milliseconds.\n");
|
|
|
|
fprintf(stderr," -d is equivalent to -d10.\n");
|
|
|
|
fprintf(stderr," -a serveraddr \n");
|
|
|
|
fprintf(stderr," -p serverport \n");
|
|
|
|
exit(1);
|
2010-04-02 18:17:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= (optind - 1);
|
|
|
|
argv += (optind - 1);
|
|
|
|
|
|
|
|
if(argc != 2 && argc != 3) {
|
2011-04-08 19:15:16 +00:00
|
|
|
err(1, "usage: %s [-B baudrate] [-H] [-L] [-s siodev] [-t tundev] [-T] [-v verbosity] [-d delay] [-a serveraddress] [-p serverport] ipaddress", prog);
|
2010-04-02 18:17:20 +00:00
|
|
|
}
|
|
|
|
ipaddr = argv[1];
|
|
|
|
|
2015-08-20 09:40:22 +00:00
|
|
|
if(baudrate != -2) { /* -2: use default baudrate */
|
|
|
|
b_rate = select_baudrate(baudrate);
|
|
|
|
if(b_rate == 0) {
|
|
|
|
err(1, "unknown baudrate %d", baudrate);
|
|
|
|
}
|
2010-04-02 18:17:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 15:29:47 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
if(*tundev == '\0') {
|
|
|
|
/* Use default. */
|
|
|
|
if(tap) {
|
|
|
|
strcpy(tundev, "tap0");
|
|
|
|
} else {
|
|
|
|
strcpy(tundev, "tun0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-05-07 12:22:41 +00:00
|
|
|
if(host != NULL) {
|
|
|
|
struct addrinfo hints, *servinfo, *p;
|
|
|
|
int rv;
|
|
|
|
char s[INET6_ADDRSTRLEN];
|
|
|
|
|
|
|
|
if(port == NULL) {
|
|
|
|
port = "60001";
|
|
|
|
}
|
2010-04-02 18:17:20 +00:00
|
|
|
|
2010-05-07 12:22:41 +00:00
|
|
|
memset(&hints, 0, sizeof hints);
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
if((rv = getaddrinfo(host, port, &hints, &servinfo)) != 0) {
|
|
|
|
err(1, "getaddrinfo: %s", gai_strerror(rv));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* loop through all the results and connect to the first we can */
|
|
|
|
for(p = servinfo; p != NULL; p = p->ai_next) {
|
|
|
|
if((slipfd = socket(p->ai_family, p->ai_socktype,
|
|
|
|
p->ai_protocol)) == -1) {
|
|
|
|
perror("client: socket");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(connect(slipfd, p->ai_addr, p->ai_addrlen) == -1) {
|
|
|
|
close(slipfd);
|
|
|
|
perror("client: connect");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(p == NULL) {
|
|
|
|
err(1, "can't connect to ``%s:%s''", host, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
fcntl(slipfd, F_SETFL, O_NONBLOCK);
|
|
|
|
|
|
|
|
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
|
|
|
|
s, sizeof(s));
|
|
|
|
fprintf(stderr, "slip connected to ``%s:%s''\n", s, port);
|
|
|
|
|
|
|
|
/* all done with this structure */
|
|
|
|
freeaddrinfo(servinfo);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if(siodev != NULL) {
|
2010-04-02 18:17:20 +00:00
|
|
|
slipfd = devopen(siodev, O_RDWR | O_NONBLOCK);
|
|
|
|
if(slipfd == -1) {
|
|
|
|
err(1, "can't open siodev ``/dev/%s''", siodev);
|
|
|
|
}
|
2010-05-07 12:22:41 +00:00
|
|
|
} else {
|
|
|
|
static const char *siodevs[] = {
|
|
|
|
"ttyUSB0", "cuaU0", "ucom0" /* linux, fbsd6, fbsd5 */
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < 3; i++) {
|
|
|
|
siodev = siodevs[i];
|
|
|
|
slipfd = devopen(siodev, O_RDWR | O_NONBLOCK);
|
|
|
|
if(slipfd != -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(slipfd == -1) {
|
|
|
|
err(1, "can't open siodev");
|
|
|
|
}
|
2010-04-02 18:17:20 +00:00
|
|
|
}
|
2010-07-30 20:44:24 +00:00
|
|
|
if (timestamp) stamptime();
|
|
|
|
fprintf(stderr, "********SLIP started on ``/dev/%s''\n", siodev);
|
2010-05-07 12:22:41 +00:00
|
|
|
stty_telos(slipfd);
|
2010-04-02 18:17:20 +00:00
|
|
|
}
|
|
|
|
slip_send(slipfd, SLIP_END);
|
|
|
|
inslip = fdopen(slipfd, "r");
|
|
|
|
if(inslip == NULL) err(1, "main: fdopen");
|
|
|
|
|
2010-04-16 12:39:46 +00:00
|
|
|
tunfd = tun_alloc(tundev, tap);
|
2014-05-09 18:25:59 +00:00
|
|
|
if(tunfd == -1) err(1, "main: open /dev/tun");
|
2010-07-30 20:44:24 +00:00
|
|
|
if (timestamp) stamptime();
|
2010-05-07 12:22:41 +00:00
|
|
|
fprintf(stderr, "opened %s device ``/dev/%s''\n",
|
|
|
|
tap ? "tap" : "tun", tundev);
|
2010-04-02 18:17:20 +00:00
|
|
|
|
|
|
|
atexit(cleanup);
|
|
|
|
signal(SIGHUP, sigcleanup);
|
|
|
|
signal(SIGTERM, sigcleanup);
|
|
|
|
signal(SIGINT, sigcleanup);
|
|
|
|
signal(SIGALRM, sigalarm);
|
|
|
|
ifconf(tundev, ipaddr);
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
maxfd = 0;
|
|
|
|
FD_ZERO(&rset);
|
|
|
|
FD_ZERO(&wset);
|
|
|
|
|
2014-05-10 21:19:51 +00:00
|
|
|
if(got_sigalarm && ipa_enable) {
|
|
|
|
/* Send "?IPA". */
|
|
|
|
slip_send(slipfd, '?');
|
|
|
|
slip_send(slipfd, 'I');
|
|
|
|
slip_send(slipfd, 'P');
|
|
|
|
slip_send(slipfd, 'A');
|
|
|
|
slip_send(slipfd, SLIP_END);
|
|
|
|
got_sigalarm = 0;
|
|
|
|
}
|
2010-04-02 18:17:20 +00:00
|
|
|
|
|
|
|
if(!slip_empty()) { /* Anything to flush? */
|
|
|
|
FD_SET(slipfd, &wset);
|
|
|
|
}
|
|
|
|
|
|
|
|
FD_SET(slipfd, &rset); /* Read from slip ASAP! */
|
|
|
|
if(slipfd > maxfd) maxfd = slipfd;
|
2014-05-10 21:03:11 +00:00
|
|
|
|
2010-04-02 18:17:20 +00:00
|
|
|
/* We only have one packet at a time queued for slip output. */
|
|
|
|
if(slip_empty()) {
|
|
|
|
FD_SET(tunfd, &rset);
|
|
|
|
if(tunfd > maxfd) maxfd = tunfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = select(maxfd + 1, &rset, &wset, NULL, NULL);
|
|
|
|
if(ret == -1 && errno != EINTR) {
|
|
|
|
err(1, "select");
|
|
|
|
} else if(ret > 0) {
|
|
|
|
if(FD_ISSET(slipfd, &rset)) {
|
|
|
|
serial_to_tun(inslip, tunfd);
|
|
|
|
}
|
2014-05-10 21:03:11 +00:00
|
|
|
|
2010-04-02 18:17:20 +00:00
|
|
|
if(FD_ISSET(slipfd, &wset)) {
|
|
|
|
slip_flushbuf(slipfd);
|
2014-05-10 21:19:51 +00:00
|
|
|
if(ipa_enable) sigalarm_reset();
|
2010-04-02 18:17:20 +00:00
|
|
|
}
|
2014-05-10 21:03:11 +00:00
|
|
|
|
2010-07-30 20:44:24 +00:00
|
|
|
/* Optional delay between outgoing packets */
|
|
|
|
/* Base delay times number of 6lowpan fragments to be sent */
|
|
|
|
if(delaymsec) {
|
|
|
|
struct timeval tv;
|
|
|
|
int dmsec;
|
|
|
|
gettimeofday(&tv, NULL) ;
|
|
|
|
dmsec=(tv.tv_sec-delaystartsec)*1000+tv.tv_usec/1000-delaystartmsec;
|
|
|
|
if(dmsec<0) delaymsec=0;
|
|
|
|
if(dmsec>delaymsec) delaymsec=0;
|
|
|
|
}
|
|
|
|
if(delaymsec==0) {
|
|
|
|
if(slip_empty() && FD_ISSET(tunfd, &rset)) {
|
2017-11-17 21:32:08 +00:00
|
|
|
tun_to_serial(tunfd, slipfd);
|
2010-07-30 20:44:24 +00:00
|
|
|
slip_flushbuf(slipfd);
|
2014-05-10 21:19:51 +00:00
|
|
|
if(ipa_enable) sigalarm_reset();
|
2010-07-30 20:44:24 +00:00
|
|
|
if(basedelay) {
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL) ;
|
|
|
|
// delaymsec=basedelay*(1+(size/120));//multiply by # of 6lowpan packets?
|
|
|
|
delaymsec=basedelay;
|
|
|
|
delaystartsec =tv.tv_sec;
|
|
|
|
delaystartmsec=tv.tv_usec/1000;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 18:17:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-01 00:45:05 +00:00
|
|
|
}
|