nos-tun.c revision 38023
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
5137669Scharnier#ifndef lint
5237669Scharnierstatic const char rcsid[] =
5338023Sbde	"$Id: nos-tun.c,v 1.3 1998/07/15 06:38:53 charnier Exp $";
5437669Scharnier#endif /* not lint */
5535137Sphk
5635734Scharnier#include <fcntl.h>
5735734Scharnier#include <netdb.h>
5835137Sphk#include <stdio.h>
5935137Sphk#include <stdlib.h>
6035137Sphk#include <string.h>
6135137Sphk#include <syslog.h>
6235734Scharnier#include <sys/signal.h>
6335137Sphk#include <sys/socket.h>
6435137Sphk#include <sys/ioctl.h>
6535137Sphk#include <netinet/in.h>
6635137Sphk#include <netinet/in_systm.h>
6735137Sphk#include <netinet/ip.h>
6835137Sphk#include <net/if.h>
6935137Sphk#include <arpa/inet.h>
7035734Scharnier#include <unistd.h>
7135137Sphk
7235137Sphk/* Tunnel interface configuration stuff */
7335137Sphkstatic struct ifaliasreq ifra;
7435137Sphkstatic struct ifreq ifrq;
7535137Sphk
7635137Sphk/* Global descriptors */
7735137Sphkint net;                          /* socket descriptor */
7835137Sphkint tun;                          /* tunnel descriptor */
7935137Sphk
8035734Scharnierstatic void usage __P((void));
8135734Scharnier
8235137Sphkint Set_address(char *addr, struct sockaddr_in *sin)
8335137Sphk{
8435137Sphk  struct hostent *hp;
8535137Sphk
8635137Sphk  bzero((char *)sin, sizeof(struct sockaddr));
8735137Sphk  sin->sin_family = AF_INET;
8835137Sphk  if((sin->sin_addr.s_addr = inet_addr(addr)) == (u_long)-1) {
8935137Sphk    hp = gethostbyname(addr);
9035137Sphk    if (!hp) {
9137669Scharnier      syslog(LOG_ERR,"unknown host %s", addr);
9235137Sphk      return 1;
9335137Sphk    }
9435137Sphk    sin->sin_family = hp->h_addrtype;
9535137Sphk    bcopy(hp->h_addr, (caddr_t)&sin->sin_addr, hp->h_length);
9635137Sphk  }
9735137Sphk  return 0;
9835137Sphk}
9935137Sphk
10035137Sphkint tun_open(char *devname, struct sockaddr *ouraddr, char *theiraddr)
10135137Sphk{
10235137Sphk  int s;
10335137Sphk  struct sockaddr_in *sin;
10435137Sphk
10535137Sphk  /* Open tun device */
10635137Sphk  tun = open (devname, O_RDWR);
10735137Sphk  if (tun < 0) {
10837669Scharnier    syslog(LOG_ERR,"can't open %s - %m",devname);
10935137Sphk    return(1);
11035137Sphk  }
11135137Sphk
11235137Sphk  /*
11335137Sphk   * At first, name the interface.
11435137Sphk   */
11535137Sphk  bzero((char *)&ifra, sizeof(ifra));
11635137Sphk  bzero((char *)&ifrq, sizeof(ifrq));
11735137Sphk
11835137Sphk  strncpy(ifrq.ifr_name, devname+5, IFNAMSIZ);
11935137Sphk  strncpy(ifra.ifra_name, devname+5, IFNAMSIZ);
12035137Sphk
12135137Sphk  s = socket(AF_INET, SOCK_DGRAM, 0);
12235137Sphk  if (s < 0) {
12338023Sbde    syslog(LOG_ERR,"can't open socket - %m");
12435137Sphk    goto tunc_return;
12535137Sphk  }
12635137Sphk
12735137Sphk  /*
12835137Sphk   *  Delete (previous) adresses for interface
12935137Sphk   *
13035137Sphk   *  !!!!
13135137Sphk   *  On FreeBSD this ioctl returns error
13235137Sphk   *  when tunN have no addresses, so - log and ignore it.
13335137Sphk   *
13435137Sphk   */
13535137Sphk  if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
13635137Sphk    syslog(LOG_ERR,"SIOCDIFADDR - %m");
13735137Sphk  }
13835137Sphk
13935137Sphk  /*
14035137Sphk   *  Set interface address
14135137Sphk   */
14235137Sphk  sin = (struct sockaddr_in *)&(ifra.ifra_addr);
14335137Sphk  bcopy(ouraddr, sin, sizeof(struct sockaddr_in));
14435137Sphk  sin->sin_len = sizeof(*sin);
14535137Sphk
14635137Sphk  /*
14735137Sphk   *  Set destination address
14835137Sphk   */
14935137Sphk  sin = (struct sockaddr_in *)&(ifra.ifra_broadaddr);
15035137Sphk  if(Set_address(theiraddr,sin)) {
15137669Scharnier    syslog(LOG_ERR,"bad destination address: %s",theiraddr);
15235137Sphk    goto stunc_return;
15335137Sphk  }
15435137Sphk  sin->sin_len = sizeof(*sin);
15535137Sphk
15635137Sphk  if (ioctl(s, SIOCAIFADDR, &ifra) < 0) {
15737669Scharnier    syslog(LOG_ERR,"can't set interface address - %m");
15835137Sphk    goto stunc_return;
15935137Sphk  }
16035137Sphk
16135137Sphk  /*
16235137Sphk   *  Now, bring up the interface.
16335137Sphk   */
16435137Sphk  if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
16537669Scharnier    syslog(LOG_ERR,"can't get interface flags - %m");
16635137Sphk    goto stunc_return;
16735137Sphk  }
16835137Sphk
16935137Sphk  ifrq.ifr_flags |= IFF_UP;
17035137Sphk  if (!(ioctl(s, SIOCSIFFLAGS, &ifrq) < 0)) {
17135137Sphk    close(s);
17235137Sphk    return(0);
17335137Sphk  }
17437669Scharnier  syslog(LOG_ERR,"can't set interface UP - %m");
17535137Sphkstunc_return:
17635137Sphk  close(s);
17735137Sphktunc_return:
17835137Sphk  close(tun);
17935137Sphk  return(1);
18035137Sphk}
18135137Sphk
18235137Sphkvoid Finish(int signum)
18335137Sphk{
18435137Sphk  int s;
18535137Sphk
18637669Scharnier  syslog(LOG_INFO,"exiting");
18735137Sphk
18835137Sphk  close(net);
18935137Sphk
19035137Sphk  s = socket(AF_INET, SOCK_DGRAM, 0);
19135137Sphk  if (s < 0) {
19237669Scharnier    syslog(LOG_ERR,"can't open socket - %m");
19335137Sphk    goto closing_tun;
19435137Sphk  }
19535137Sphk
19635137Sphk  /*
19735137Sphk   *  Shut down interface.
19835137Sphk   */
19935137Sphk  if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
20037669Scharnier    syslog(LOG_ERR,"can't get interface flags - %m");
20135137Sphk    goto closing_fds;
20235137Sphk  }
20335137Sphk
20435137Sphk  ifrq.ifr_flags &= ~(IFF_UP|IFF_RUNNING);
20535137Sphk  if (ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
20637669Scharnier    syslog(LOG_ERR,"can't set interface DOWN - %m");
20735137Sphk    goto closing_fds;
20835137Sphk  }
20935137Sphk
21035137Sphk  /*
21135137Sphk   *  Delete adresses for interface
21235137Sphk   */
21335137Sphk  bzero(&ifra.ifra_addr, sizeof(ifra.ifra_addr));
21435137Sphk  bzero(&ifra.ifra_broadaddr, sizeof(ifra.ifra_addr));
21535137Sphk  bzero(&ifra.ifra_mask, sizeof(ifra.ifra_addr));
21635137Sphk  if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
21737669Scharnier    syslog(LOG_ERR,"can't delete interface's addresses - %m");
21835137Sphk  }
21935137Sphkclosing_fds:
22035137Sphk  close(s);
22135137Sphkclosing_tun:
22235137Sphk  close(tun);
22335137Sphk  closelog();
22435137Sphk  exit(signum);
22535137Sphk}
22635137Sphk
22735137Sphkint main (int argc, char **argv)
22835137Sphk{
22935137Sphk  int  c, len, ipoff;
23035137Sphk
23135137Sphk  char *devname = NULL;
23235137Sphk  char *point_to = NULL;
23335137Sphk  char *to_point = NULL;
23435137Sphk  char *target;
23535137Sphk
23635137Sphk  struct sockaddr t_laddr;          /* Source address of tunnel */
23735137Sphk  struct sockaddr whereto;          /* Destination of tunnel */
23835137Sphk  struct sockaddr_in *to;
23935137Sphk
24035137Sphk  char buf[0x2000];                 /* Packets buffer */
24135137Sphk  struct ip *ip = (struct ip *)buf;
24235137Sphk
24335137Sphk  fd_set rfds, wfds, efds;          /* File descriptors for select() */
24435137Sphk  int nfds;                         /* Return from select() */
24535137Sphk
24635137Sphk
24735734Scharnier  while ((c = getopt(argc, argv, "d:s:t:")) != -1) {
24835137Sphk    switch (c) {
24935137Sphk    case 'd':
25035137Sphk      to_point = optarg;
25135137Sphk      break;
25235137Sphk    case 's':
25335137Sphk      point_to = optarg;
25435137Sphk      break;
25535137Sphk    case 't':
25635137Sphk      devname = optarg;
25735137Sphk      break;
25835137Sphk    }
25935137Sphk  }
26035137Sphk  argc -= optind;
26135137Sphk  argv += optind;
26235137Sphk
26335137Sphk  if (argc != 1 || (devname == NULL) ||
26435137Sphk      (point_to == NULL) || (to_point == NULL)) {
26535734Scharnier    usage();
26635137Sphk  }
26735137Sphk
26835137Sphk  target = *argv;
26935137Sphk
27035137Sphk  /* Establish logging through 'syslog' */
27135137Sphk  openlog("nos_tun", LOG_PID, LOG_DAEMON);
27235137Sphk
27335137Sphk  if(Set_address(point_to, (struct sockaddr_in *)&t_laddr)) {
27435137Sphk    closelog();
27535137Sphk    exit(2);
27635137Sphk  }
27735137Sphk
27835137Sphk  if(tun_open(devname, &t_laddr, to_point)) {
27935137Sphk    closelog();
28035137Sphk    exit(3);
28135137Sphk  }
28235137Sphk
28335137Sphk  to = (struct sockaddr_in *)&whereto;
28435137Sphk  if(Set_address(target, to))
28535137Sphk    Finish(4);
28635137Sphk
28735137Sphk  if ((net = socket(AF_INET, SOCK_RAW, 94)) < 0) {
28837669Scharnier    syslog(LOG_ERR,"can't open socket - %m");
28935137Sphk    Finish(5);
29035137Sphk  }
29135137Sphk
29235137Sphk  if (connect(net,&whereto,sizeof(struct sockaddr_in)) < 0 ) {
29337669Scharnier    syslog(LOG_ERR,"can't connect to target - %m");
29435137Sphk    close(net);
29535137Sphk    Finish(6);
29635137Sphk  }
29735137Sphk
29835137Sphk  /*  Demonize it */
29935137Sphk  daemon(0,0);
30035137Sphk
30135137Sphk  /* Install signal handlers */
30235137Sphk  (void)signal(SIGHUP,Finish);
30335137Sphk  (void)signal(SIGINT,Finish);
30435137Sphk  (void)signal(SIGTERM,Finish);
30535137Sphk
30635137Sphk  for (;;) {
30735137Sphk    /* Set file descriptors for select() */
30835137Sphk    FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds);
30935137Sphk    FD_SET(tun,&rfds); FD_SET(net,&rfds);
31035137Sphk
31135137Sphk    nfds = select(net+10,&rfds,&wfds,&efds,NULL);
31235137Sphk    if(nfds < 0) {
31337669Scharnier      syslog(LOG_ERR,"interrupted select");
31435137Sphk      close(net);
31535137Sphk      Finish(7);
31635137Sphk    }
31735137Sphk    if(nfds == 0) {         /* Impossible ? */
31837669Scharnier      syslog(LOG_ERR,"timeout in select");
31935137Sphk      close(net);
32035137Sphk      Finish(8);
32135137Sphk    }
32235137Sphk
32335137Sphk
32435137Sphk    if(FD_ISSET(net,&rfds)) {
32535137Sphk      /* Read from socket ... */
32635137Sphk      len = read(net, buf, sizeof(buf));
32735137Sphk      /* Check if this is "our" packet */
32835137Sphk      if((ip->ip_src).s_addr == (to->sin_addr).s_addr) {
32935137Sphk	/* ... skip encapsulation headers ... */
33035137Sphk	ipoff = (ip->ip_hl << 2);
33135137Sphk	/* ... and write to tun-device */
33235137Sphk	write(tun,buf+ipoff,len-ipoff);
33335137Sphk      }
33435137Sphk    }
33535137Sphk
33635137Sphk    if(FD_ISSET(tun,&rfds)) {
33735137Sphk      /* Read from tun ... */
33835137Sphk      len = read(tun, buf, sizeof(buf));
33935137Sphk      /* ... and send to network */
34035137Sphk      if(send(net, buf, len,0) <= 0) {
34137669Scharnier	syslog(LOG_ERR,"can't send - %m");
34235137Sphk      }
34335137Sphk    }
34435137Sphk  }
34535137Sphk}
34635137Sphk
34735734Scharnierstatic void
34835734Scharnierusage()
34935734Scharnier{
35035734Scharnier	fprintf(stderr,
35135734Scharnier"usage: nos_tun -t <tun_name> -s <source_addr> -d <dest_addr> <target_addr>\n");
35235734Scharnier	exit(1);
35335734Scharnier}
35435734Scharnier
355