nos-tun.c revision 50476
135137Sphk/* 235137Sphk * Copyright (c) 1996, Nickolay Dudorov 335137Sphk * All rights reserved. 435137Sphk * 535137Sphk * Redistribution and use in source and binary forms, with or without 635137Sphk * modification, are permitted provided that the following conditions 735137Sphk * are met: 835137Sphk * 1. Redistributions of source code must retain the above copyright 935137Sphk * notice unmodified, this list of conditions, and the following 1035137Sphk * disclaimer. 1135137Sphk * 2. Redistributions in binary form must reproduce the above copyright 1235137Sphk * notice, this list of conditions and the following disclaimer in the 1335137Sphk * documentation and/or other materials provided with the distribution. 1435137Sphk * 1535137Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1635137Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1735137Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1835137Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1935137Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2035137Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2135137Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2235137Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2335137Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2435137Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2535137Sphk * SUCH DAMAGE. 2635137Sphk * 2735137Sphk */ 2835137Sphk 2935137Sphk/* 3035137Sphk * 'nos_tun' program configure tunN interface as a point-to-point 3135137Sphk * connection with two "pseudo"-addresses between this host and 3235137Sphk * 'target'. 3335137Sphk * 3435137Sphk * It uses Ip-over-Ip incapsulation ( protocol number 94 - IPIP) 3535137Sphk * (known as NOS-incapsulation in CISCO-routers' terminology). 3635137Sphk * 3735137Sphk * 'nos_tun' can works with itself and CISCO-routers. 3835137Sphk * (It may also work with Linux 'nos_tun's, but 3935137Sphk * I have no Linux system here to test with). 4035137Sphk * 4135137Sphk * BUGS (or features ?): 4235137Sphk * - you must specify ONE of the target host's addresses 4335137Sphk * ( nos_tun sends and accepts packets only to/from this 4435137Sphk * address ) 4535137Sphk * - there can be only ONE tunnel between two hosts, 4635137Sphk * more precisely - between given host and (one of) 4735137Sphk * target hosts' address(es) 4835137Sphk * (and why do you want more ?) 4935137Sphk */ 5035137Sphk 5145114Sphk/* 5245114Sphk * Mar. 23 1999 by Isao SEKI <iseki@gongon.com> 5345114Sphk * I added a new flag for ip protocol number. 5445114Sphk * We are using 4 as protocol number in ampr.org. 5545114Sphk * 5645114Sphk */ 5745114Sphk 5837669Scharnier#ifndef lint 5937669Scharnierstatic const char rcsid[] = 6050476Speter "$FreeBSD: head/sbin/nos-tun/nos-tun.c 50476 1999-08-28 00:22:10Z peter $"; 6137669Scharnier#endif /* not lint */ 6235137Sphk 6335734Scharnier#include <fcntl.h> 6435734Scharnier#include <netdb.h> 6535137Sphk#include <stdio.h> 6635137Sphk#include <stdlib.h> 6735137Sphk#include <string.h> 6835137Sphk#include <syslog.h> 6935734Scharnier#include <sys/signal.h> 7035137Sphk#include <sys/socket.h> 7135137Sphk#include <sys/ioctl.h> 7235137Sphk#include <netinet/in.h> 7335137Sphk#include <netinet/in_systm.h> 7435137Sphk#include <netinet/ip.h> 7535137Sphk#include <net/if.h> 7635137Sphk#include <arpa/inet.h> 7735734Scharnier#include <unistd.h> 7835137Sphk 7935137Sphk/* Tunnel interface configuration stuff */ 8035137Sphkstatic struct ifaliasreq ifra; 8135137Sphkstatic struct ifreq ifrq; 8235137Sphk 8335137Sphk/* Global descriptors */ 8435137Sphkint net; /* socket descriptor */ 8535137Sphkint tun; /* tunnel descriptor */ 8635137Sphk 8735734Scharnierstatic void usage __P((void)); 8835734Scharnier 8935137Sphkint Set_address(char *addr, struct sockaddr_in *sin) 9035137Sphk{ 9135137Sphk struct hostent *hp; 9235137Sphk 9335137Sphk bzero((char *)sin, sizeof(struct sockaddr)); 9435137Sphk sin->sin_family = AF_INET; 9535137Sphk if((sin->sin_addr.s_addr = inet_addr(addr)) == (u_long)-1) { 9635137Sphk hp = gethostbyname(addr); 9735137Sphk if (!hp) { 9837669Scharnier syslog(LOG_ERR,"unknown host %s", addr); 9935137Sphk return 1; 10035137Sphk } 10135137Sphk sin->sin_family = hp->h_addrtype; 10235137Sphk bcopy(hp->h_addr, (caddr_t)&sin->sin_addr, hp->h_length); 10335137Sphk } 10435137Sphk return 0; 10535137Sphk} 10635137Sphk 10735137Sphkint tun_open(char *devname, struct sockaddr *ouraddr, char *theiraddr) 10835137Sphk{ 10935137Sphk int s; 11035137Sphk struct sockaddr_in *sin; 11135137Sphk 11235137Sphk /* Open tun device */ 11335137Sphk tun = open (devname, O_RDWR); 11435137Sphk if (tun < 0) { 11537669Scharnier syslog(LOG_ERR,"can't open %s - %m",devname); 11635137Sphk return(1); 11735137Sphk } 11835137Sphk 11935137Sphk /* 12035137Sphk * At first, name the interface. 12135137Sphk */ 12235137Sphk bzero((char *)&ifra, sizeof(ifra)); 12335137Sphk bzero((char *)&ifrq, sizeof(ifrq)); 12435137Sphk 12535137Sphk strncpy(ifrq.ifr_name, devname+5, IFNAMSIZ); 12635137Sphk strncpy(ifra.ifra_name, devname+5, IFNAMSIZ); 12735137Sphk 12835137Sphk s = socket(AF_INET, SOCK_DGRAM, 0); 12935137Sphk if (s < 0) { 13038023Sbde syslog(LOG_ERR,"can't open socket - %m"); 13135137Sphk goto tunc_return; 13235137Sphk } 13335137Sphk 13435137Sphk /* 13535137Sphk * Delete (previous) adresses for interface 13635137Sphk * 13735137Sphk * !!!! 13835137Sphk * On FreeBSD this ioctl returns error 13935137Sphk * when tunN have no addresses, so - log and ignore it. 14035137Sphk * 14135137Sphk */ 14235137Sphk if (ioctl(s, SIOCDIFADDR, &ifra) < 0) { 14335137Sphk syslog(LOG_ERR,"SIOCDIFADDR - %m"); 14435137Sphk } 14535137Sphk 14635137Sphk /* 14735137Sphk * Set interface address 14835137Sphk */ 14935137Sphk sin = (struct sockaddr_in *)&(ifra.ifra_addr); 15035137Sphk bcopy(ouraddr, sin, sizeof(struct sockaddr_in)); 15135137Sphk sin->sin_len = sizeof(*sin); 15235137Sphk 15335137Sphk /* 15435137Sphk * Set destination address 15535137Sphk */ 15635137Sphk sin = (struct sockaddr_in *)&(ifra.ifra_broadaddr); 15735137Sphk if(Set_address(theiraddr,sin)) { 15837669Scharnier syslog(LOG_ERR,"bad destination address: %s",theiraddr); 15935137Sphk goto stunc_return; 16035137Sphk } 16135137Sphk sin->sin_len = sizeof(*sin); 16235137Sphk 16335137Sphk if (ioctl(s, SIOCAIFADDR, &ifra) < 0) { 16437669Scharnier syslog(LOG_ERR,"can't set interface address - %m"); 16535137Sphk goto stunc_return; 16635137Sphk } 16735137Sphk 16835137Sphk /* 16935137Sphk * Now, bring up the interface. 17035137Sphk */ 17135137Sphk if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 17237669Scharnier syslog(LOG_ERR,"can't get interface flags - %m"); 17335137Sphk goto stunc_return; 17435137Sphk } 17535137Sphk 17635137Sphk ifrq.ifr_flags |= IFF_UP; 17735137Sphk if (!(ioctl(s, SIOCSIFFLAGS, &ifrq) < 0)) { 17835137Sphk close(s); 17935137Sphk return(0); 18035137Sphk } 18137669Scharnier syslog(LOG_ERR,"can't set interface UP - %m"); 18235137Sphkstunc_return: 18335137Sphk close(s); 18435137Sphktunc_return: 18535137Sphk close(tun); 18635137Sphk return(1); 18735137Sphk} 18835137Sphk 18935137Sphkvoid Finish(int signum) 19035137Sphk{ 19135137Sphk int s; 19235137Sphk 19337669Scharnier syslog(LOG_INFO,"exiting"); 19435137Sphk 19535137Sphk close(net); 19635137Sphk 19735137Sphk s = socket(AF_INET, SOCK_DGRAM, 0); 19835137Sphk if (s < 0) { 19937669Scharnier syslog(LOG_ERR,"can't open socket - %m"); 20035137Sphk goto closing_tun; 20135137Sphk } 20235137Sphk 20335137Sphk /* 20435137Sphk * Shut down interface. 20535137Sphk */ 20635137Sphk if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 20737669Scharnier syslog(LOG_ERR,"can't get interface flags - %m"); 20835137Sphk goto closing_fds; 20935137Sphk } 21035137Sphk 21135137Sphk ifrq.ifr_flags &= ~(IFF_UP|IFF_RUNNING); 21235137Sphk if (ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 21337669Scharnier syslog(LOG_ERR,"can't set interface DOWN - %m"); 21435137Sphk goto closing_fds; 21535137Sphk } 21635137Sphk 21735137Sphk /* 21835137Sphk * Delete adresses for interface 21935137Sphk */ 22035137Sphk bzero(&ifra.ifra_addr, sizeof(ifra.ifra_addr)); 22135137Sphk bzero(&ifra.ifra_broadaddr, sizeof(ifra.ifra_addr)); 22235137Sphk bzero(&ifra.ifra_mask, sizeof(ifra.ifra_addr)); 22335137Sphk if (ioctl(s, SIOCDIFADDR, &ifra) < 0) { 22437669Scharnier syslog(LOG_ERR,"can't delete interface's addresses - %m"); 22535137Sphk } 22635137Sphkclosing_fds: 22735137Sphk close(s); 22835137Sphkclosing_tun: 22935137Sphk close(tun); 23035137Sphk closelog(); 23135137Sphk exit(signum); 23235137Sphk} 23335137Sphk 23435137Sphkint main (int argc, char **argv) 23535137Sphk{ 23635137Sphk int c, len, ipoff; 23735137Sphk 23835137Sphk char *devname = NULL; 23935137Sphk char *point_to = NULL; 24035137Sphk char *to_point = NULL; 24135137Sphk char *target; 24245114Sphk char *protocol = NULL; 24345114Sphk int protnum; 24435137Sphk 24535137Sphk struct sockaddr t_laddr; /* Source address of tunnel */ 24635137Sphk struct sockaddr whereto; /* Destination of tunnel */ 24735137Sphk struct sockaddr_in *to; 24835137Sphk 24935137Sphk char buf[0x2000]; /* Packets buffer */ 25035137Sphk struct ip *ip = (struct ip *)buf; 25135137Sphk 25235137Sphk fd_set rfds, wfds, efds; /* File descriptors for select() */ 25335137Sphk int nfds; /* Return from select() */ 25435137Sphk 25535137Sphk 25645114Sphk while ((c = getopt(argc, argv, "d:s:t:p:")) != -1) { 25735137Sphk switch (c) { 25835137Sphk case 'd': 25935137Sphk to_point = optarg; 26035137Sphk break; 26135137Sphk case 's': 26235137Sphk point_to = optarg; 26335137Sphk break; 26435137Sphk case 't': 26535137Sphk devname = optarg; 26635137Sphk break; 26745114Sphk case 'p': 26845114Sphk protocol = optarg; 26945114Sphk break; 27035137Sphk } 27135137Sphk } 27235137Sphk argc -= optind; 27335137Sphk argv += optind; 27435137Sphk 27535137Sphk if (argc != 1 || (devname == NULL) || 27635137Sphk (point_to == NULL) || (to_point == NULL)) { 27735734Scharnier usage(); 27835137Sphk } 27935137Sphk 28045114Sphk if(protocol == NULL) 28145114Sphk protnum = 94; 28245114Sphk else 28345114Sphk protnum = atoi(protocol); 28445114Sphk 28535137Sphk target = *argv; 28635137Sphk 28735137Sphk /* Establish logging through 'syslog' */ 28835137Sphk openlog("nos_tun", LOG_PID, LOG_DAEMON); 28935137Sphk 29035137Sphk if(Set_address(point_to, (struct sockaddr_in *)&t_laddr)) { 29135137Sphk closelog(); 29235137Sphk exit(2); 29335137Sphk } 29435137Sphk 29535137Sphk if(tun_open(devname, &t_laddr, to_point)) { 29635137Sphk closelog(); 29735137Sphk exit(3); 29835137Sphk } 29935137Sphk 30035137Sphk to = (struct sockaddr_in *)&whereto; 30135137Sphk if(Set_address(target, to)) 30235137Sphk Finish(4); 30335137Sphk 30445114Sphk if ((net = socket(AF_INET, SOCK_RAW, protnum)) < 0) { 30537669Scharnier syslog(LOG_ERR,"can't open socket - %m"); 30635137Sphk Finish(5); 30735137Sphk } 30835137Sphk 30935137Sphk if (connect(net,&whereto,sizeof(struct sockaddr_in)) < 0 ) { 31037669Scharnier syslog(LOG_ERR,"can't connect to target - %m"); 31135137Sphk close(net); 31235137Sphk Finish(6); 31335137Sphk } 31435137Sphk 31535137Sphk /* Demonize it */ 31635137Sphk daemon(0,0); 31735137Sphk 31835137Sphk /* Install signal handlers */ 31935137Sphk (void)signal(SIGHUP,Finish); 32035137Sphk (void)signal(SIGINT,Finish); 32135137Sphk (void)signal(SIGTERM,Finish); 32235137Sphk 32335137Sphk for (;;) { 32435137Sphk /* Set file descriptors for select() */ 32535137Sphk FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds); 32635137Sphk FD_SET(tun,&rfds); FD_SET(net,&rfds); 32735137Sphk 32835137Sphk nfds = select(net+10,&rfds,&wfds,&efds,NULL); 32935137Sphk if(nfds < 0) { 33037669Scharnier syslog(LOG_ERR,"interrupted select"); 33135137Sphk close(net); 33235137Sphk Finish(7); 33335137Sphk } 33435137Sphk if(nfds == 0) { /* Impossible ? */ 33537669Scharnier syslog(LOG_ERR,"timeout in select"); 33635137Sphk close(net); 33735137Sphk Finish(8); 33835137Sphk } 33935137Sphk 34035137Sphk 34135137Sphk if(FD_ISSET(net,&rfds)) { 34235137Sphk /* Read from socket ... */ 34335137Sphk len = read(net, buf, sizeof(buf)); 34435137Sphk /* Check if this is "our" packet */ 34535137Sphk if((ip->ip_src).s_addr == (to->sin_addr).s_addr) { 34635137Sphk /* ... skip encapsulation headers ... */ 34735137Sphk ipoff = (ip->ip_hl << 2); 34835137Sphk /* ... and write to tun-device */ 34935137Sphk write(tun,buf+ipoff,len-ipoff); 35035137Sphk } 35135137Sphk } 35235137Sphk 35335137Sphk if(FD_ISSET(tun,&rfds)) { 35435137Sphk /* Read from tun ... */ 35535137Sphk len = read(tun, buf, sizeof(buf)); 35635137Sphk /* ... and send to network */ 35735137Sphk if(send(net, buf, len,0) <= 0) { 35837669Scharnier syslog(LOG_ERR,"can't send - %m"); 35935137Sphk } 36035137Sphk } 36135137Sphk } 36235137Sphk} 36335137Sphk 36435734Scharnierstatic void 36535734Scharnierusage() 36635734Scharnier{ 36735734Scharnier fprintf(stderr, 36845114Sphk"usage: nos_tun -t <tun_name> -s <source_addr> -d <dest_addr> -p <protocol_number> <target_addr>\n"); 36935734Scharnier exit(1); 37035734Scharnier} 37135734Scharnier 372