nos-tun.c revision 121540
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/*
3061438Sasmodai *  '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 *
3761438Sasmodai *  'nos-tun' can works with itself and CISCO-routers.
3861438Sasmodai *  (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
4361438Sasmodai *    ( 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 121540 2003-10-26 04:37:57Z peter $";
6137669Scharnier#endif /* not lint */
6235137Sphk
63103417Smike#include <sys/types.h>
64103417Smike#include <sys/ioctl.h>
65103417Smike#include <sys/signal.h>
66103417Smike#include <sys/socket.h>
67103417Smike
68103417Smike#include <net/if.h>
69103417Smike#include <netinet/in.h>
70103417Smike#include <netinet/in_systm.h>
71103417Smike#include <netinet/ip.h>
72103417Smike
73103417Smike#include <arpa/inet.h>
7435734Scharnier#include <fcntl.h>
7535734Scharnier#include <netdb.h>
7635137Sphk#include <stdio.h>
7735137Sphk#include <stdlib.h>
7835137Sphk#include <string.h>
7935137Sphk#include <syslog.h>
8035734Scharnier#include <unistd.h>
8135137Sphk
8235137Sphk/* Tunnel interface configuration stuff */
8335137Sphkstatic struct ifaliasreq ifra;
8435137Sphkstatic struct ifreq ifrq;
8535137Sphk
8635137Sphk/* Global descriptors */
8735137Sphkint net;                          /* socket descriptor */
8835137Sphkint tun;                          /* tunnel descriptor */
8935137Sphk
9092883Simpstatic void usage(void);
9135734Scharnier
9235137Sphkint Set_address(char *addr, struct sockaddr_in *sin)
9335137Sphk{
9435137Sphk  struct hostent *hp;
9535137Sphk
9635137Sphk  bzero((char *)sin, sizeof(struct sockaddr));
9735137Sphk  sin->sin_family = AF_INET;
98121540Speter  if((sin->sin_addr.s_addr = inet_addr(addr)) == (in_addr_t)-1) {
9935137Sphk    hp = gethostbyname(addr);
10035137Sphk    if (!hp) {
10137669Scharnier      syslog(LOG_ERR,"unknown host %s", addr);
10235137Sphk      return 1;
10335137Sphk    }
10435137Sphk    sin->sin_family = hp->h_addrtype;
10535137Sphk    bcopy(hp->h_addr, (caddr_t)&sin->sin_addr, hp->h_length);
10635137Sphk  }
10735137Sphk  return 0;
10835137Sphk}
10935137Sphk
11035137Sphkint tun_open(char *devname, struct sockaddr *ouraddr, char *theiraddr)
11135137Sphk{
11235137Sphk  int s;
11335137Sphk  struct sockaddr_in *sin;
11435137Sphk
11535137Sphk  /* Open tun device */
11635137Sphk  tun = open (devname, O_RDWR);
11735137Sphk  if (tun < 0) {
11837669Scharnier    syslog(LOG_ERR,"can't open %s - %m",devname);
11935137Sphk    return(1);
12035137Sphk  }
12135137Sphk
12235137Sphk  /*
12335137Sphk   * At first, name the interface.
12435137Sphk   */
12535137Sphk  bzero((char *)&ifra, sizeof(ifra));
12635137Sphk  bzero((char *)&ifrq, sizeof(ifrq));
12735137Sphk
12835137Sphk  strncpy(ifrq.ifr_name, devname+5, IFNAMSIZ);
12935137Sphk  strncpy(ifra.ifra_name, devname+5, IFNAMSIZ);
13035137Sphk
13135137Sphk  s = socket(AF_INET, SOCK_DGRAM, 0);
13235137Sphk  if (s < 0) {
13338023Sbde    syslog(LOG_ERR,"can't open socket - %m");
13435137Sphk    goto tunc_return;
13535137Sphk  }
13635137Sphk
13735137Sphk  /*
13880203Skris   *  Delete (previous) addresses for interface
13935137Sphk   *
14035137Sphk   *  !!!!
14135137Sphk   *  On FreeBSD this ioctl returns error
14235137Sphk   *  when tunN have no addresses, so - log and ignore it.
14335137Sphk   *
14435137Sphk   */
14535137Sphk  if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
14635137Sphk    syslog(LOG_ERR,"SIOCDIFADDR - %m");
14735137Sphk  }
14835137Sphk
14935137Sphk  /*
15035137Sphk   *  Set interface address
15135137Sphk   */
15235137Sphk  sin = (struct sockaddr_in *)&(ifra.ifra_addr);
15335137Sphk  bcopy(ouraddr, sin, sizeof(struct sockaddr_in));
15435137Sphk  sin->sin_len = sizeof(*sin);
15535137Sphk
15635137Sphk  /*
15735137Sphk   *  Set destination address
15835137Sphk   */
15935137Sphk  sin = (struct sockaddr_in *)&(ifra.ifra_broadaddr);
16035137Sphk  if(Set_address(theiraddr,sin)) {
16137669Scharnier    syslog(LOG_ERR,"bad destination address: %s",theiraddr);
16235137Sphk    goto stunc_return;
16335137Sphk  }
16435137Sphk  sin->sin_len = sizeof(*sin);
16535137Sphk
16635137Sphk  if (ioctl(s, SIOCAIFADDR, &ifra) < 0) {
16737669Scharnier    syslog(LOG_ERR,"can't set interface address - %m");
16835137Sphk    goto stunc_return;
16935137Sphk  }
17035137Sphk
17135137Sphk  /*
17235137Sphk   *  Now, bring up the interface.
17335137Sphk   */
17435137Sphk  if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
17537669Scharnier    syslog(LOG_ERR,"can't get interface flags - %m");
17635137Sphk    goto stunc_return;
17735137Sphk  }
17835137Sphk
17935137Sphk  ifrq.ifr_flags |= IFF_UP;
18035137Sphk  if (!(ioctl(s, SIOCSIFFLAGS, &ifrq) < 0)) {
18135137Sphk    close(s);
18235137Sphk    return(0);
18335137Sphk  }
18437669Scharnier  syslog(LOG_ERR,"can't set interface UP - %m");
18535137Sphkstunc_return:
18635137Sphk  close(s);
18735137Sphktunc_return:
18835137Sphk  close(tun);
18935137Sphk  return(1);
19035137Sphk}
19135137Sphk
19235137Sphkvoid Finish(int signum)
19335137Sphk{
19435137Sphk  int s;
19535137Sphk
19637669Scharnier  syslog(LOG_INFO,"exiting");
19735137Sphk
19835137Sphk  close(net);
19935137Sphk
20035137Sphk  s = socket(AF_INET, SOCK_DGRAM, 0);
20135137Sphk  if (s < 0) {
20237669Scharnier    syslog(LOG_ERR,"can't open socket - %m");
20335137Sphk    goto closing_tun;
20435137Sphk  }
20535137Sphk
20635137Sphk  /*
20735137Sphk   *  Shut down interface.
20835137Sphk   */
20935137Sphk  if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
21037669Scharnier    syslog(LOG_ERR,"can't get interface flags - %m");
21135137Sphk    goto closing_fds;
21235137Sphk  }
21335137Sphk
21435137Sphk  ifrq.ifr_flags &= ~(IFF_UP|IFF_RUNNING);
21535137Sphk  if (ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
21637669Scharnier    syslog(LOG_ERR,"can't set interface DOWN - %m");
21735137Sphk    goto closing_fds;
21835137Sphk  }
21935137Sphk
22035137Sphk  /*
22180203Skris   *  Delete addresses for interface
22235137Sphk   */
22335137Sphk  bzero(&ifra.ifra_addr, sizeof(ifra.ifra_addr));
22435137Sphk  bzero(&ifra.ifra_broadaddr, sizeof(ifra.ifra_addr));
22535137Sphk  bzero(&ifra.ifra_mask, sizeof(ifra.ifra_addr));
22635137Sphk  if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
22737669Scharnier    syslog(LOG_ERR,"can't delete interface's addresses - %m");
22835137Sphk  }
22935137Sphkclosing_fds:
23035137Sphk  close(s);
23135137Sphkclosing_tun:
23235137Sphk  close(tun);
23335137Sphk  closelog();
23435137Sphk  exit(signum);
23535137Sphk}
23635137Sphk
23735137Sphkint main (int argc, char **argv)
23835137Sphk{
23935137Sphk  int  c, len, ipoff;
24035137Sphk
24135137Sphk  char *devname = NULL;
24235137Sphk  char *point_to = NULL;
24335137Sphk  char *to_point = NULL;
24435137Sphk  char *target;
24574948Sphk  char *source = NULL;
24645114Sphk  char *protocol = NULL;
24745114Sphk  int protnum;
24835137Sphk
24935137Sphk  struct sockaddr t_laddr;          /* Source address of tunnel */
25035137Sphk  struct sockaddr whereto;          /* Destination of tunnel */
25174948Sphk  struct sockaddr wherefrom;        /* Source of tunnel */
25235137Sphk  struct sockaddr_in *to;
25335137Sphk
25435137Sphk  char buf[0x2000];                 /* Packets buffer */
25535137Sphk  struct ip *ip = (struct ip *)buf;
25635137Sphk
25789583Sbillf  fd_set rfds;                      /* File descriptors for select() */
25835137Sphk  int nfds;                         /* Return from select() */
25989583Sbillf  int lastfd;                       /* highest fd we care about */
26035137Sphk
26135137Sphk
26245114Sphk  while ((c = getopt(argc, argv, "d:s:t:p:")) != -1) {
26335137Sphk    switch (c) {
26435137Sphk    case 'd':
26535137Sphk      to_point = optarg;
26635137Sphk      break;
26735137Sphk    case 's':
26835137Sphk      point_to = optarg;
26935137Sphk      break;
27035137Sphk    case 't':
27135137Sphk      devname = optarg;
27235137Sphk      break;
27345114Sphk    case 'p':
27445114Sphk      protocol = optarg;
27545114Sphk      break;
27635137Sphk    }
27735137Sphk  }
27835137Sphk  argc -= optind;
27935137Sphk  argv += optind;
28035137Sphk
28174948Sphk  if ((argc != 1 && argc != 2) || (devname == NULL) ||
28235137Sphk      (point_to == NULL) || (to_point == NULL)) {
28335734Scharnier    usage();
28435137Sphk  }
28535137Sphk
28645114Sphk  if(protocol == NULL)
28745114Sphk      protnum = 94;
28845114Sphk  else
28945114Sphk      protnum = atoi(protocol);
29045114Sphk
29174948Sphk  if (argc == 1) {
29274948Sphk      target = *argv;
29374948Sphk  } else {
29474948Sphk      source = *argv++; target = *argv;
29574948Sphk  }
29635137Sphk
29735137Sphk  /* Establish logging through 'syslog' */
29861438Sasmodai  openlog("nos-tun", LOG_PID, LOG_DAEMON);
29935137Sphk
30035137Sphk  if(Set_address(point_to, (struct sockaddr_in *)&t_laddr)) {
30135137Sphk    closelog();
30235137Sphk    exit(2);
30335137Sphk  }
30435137Sphk
30535137Sphk  if(tun_open(devname, &t_laddr, to_point)) {
30635137Sphk    closelog();
30735137Sphk    exit(3);
30835137Sphk  }
30935137Sphk
31035137Sphk  to = (struct sockaddr_in *)&whereto;
31135137Sphk  if(Set_address(target, to))
31235137Sphk    Finish(4);
31335137Sphk
31445114Sphk  if ((net = socket(AF_INET, SOCK_RAW, protnum)) < 0) {
31537669Scharnier    syslog(LOG_ERR,"can't open socket - %m");
31635137Sphk    Finish(5);
31735137Sphk  }
31835137Sphk
31974948Sphk  if (source) {
32074948Sphk	if (Set_address(source, (struct sockaddr_in *)&wherefrom))
32174948Sphk	  Finish(9);
32274948Sphk    if (bind(net, &wherefrom, sizeof(wherefrom)) < 0) {
32374948Sphk	  syslog(LOG_ERR, "can't bind source address - %m");
32474948Sphk	  Finish(10);
32574948Sphk	}
32674948Sphk  }
32774948Sphk
32835137Sphk  if (connect(net,&whereto,sizeof(struct sockaddr_in)) < 0 ) {
32937669Scharnier    syslog(LOG_ERR,"can't connect to target - %m");
33035137Sphk    close(net);
33135137Sphk    Finish(6);
33235137Sphk  }
33335137Sphk
33435137Sphk  /*  Demonize it */
33535137Sphk  daemon(0,0);
33635137Sphk
33735137Sphk  /* Install signal handlers */
33835137Sphk  (void)signal(SIGHUP,Finish);
33935137Sphk  (void)signal(SIGINT,Finish);
34035137Sphk  (void)signal(SIGTERM,Finish);
34135137Sphk
34289583Sbillf  if (tun > net)
34389583Sbillf	lastfd = tun;
34489583Sbillf  else
34589583Sbillf	lastfd = net;
34689583Sbillf
34735137Sphk  for (;;) {
34835137Sphk    /* Set file descriptors for select() */
34989583Sbillf    FD_ZERO(&rfds);
35035137Sphk    FD_SET(tun,&rfds); FD_SET(net,&rfds);
35135137Sphk
35289583Sbillf    nfds = select(lastfd+1,&rfds,NULL,NULL,NULL);
35335137Sphk    if(nfds < 0) {
35437669Scharnier      syslog(LOG_ERR,"interrupted select");
35535137Sphk      close(net);
35635137Sphk      Finish(7);
35735137Sphk    }
35835137Sphk    if(nfds == 0) {         /* Impossible ? */
35937669Scharnier      syslog(LOG_ERR,"timeout in select");
36035137Sphk      close(net);
36135137Sphk      Finish(8);
36235137Sphk    }
36335137Sphk
36435137Sphk
36535137Sphk    if(FD_ISSET(net,&rfds)) {
36635137Sphk      /* Read from socket ... */
36735137Sphk      len = read(net, buf, sizeof(buf));
36835137Sphk      /* Check if this is "our" packet */
36935137Sphk      if((ip->ip_src).s_addr == (to->sin_addr).s_addr) {
37035137Sphk	/* ... skip encapsulation headers ... */
37135137Sphk	ipoff = (ip->ip_hl << 2);
37235137Sphk	/* ... and write to tun-device */
37335137Sphk	write(tun,buf+ipoff,len-ipoff);
37435137Sphk      }
37535137Sphk    }
37635137Sphk
37735137Sphk    if(FD_ISSET(tun,&rfds)) {
37835137Sphk      /* Read from tun ... */
37935137Sphk      len = read(tun, buf, sizeof(buf));
38035137Sphk      /* ... and send to network */
38135137Sphk      if(send(net, buf, len,0) <= 0) {
38237669Scharnier	syslog(LOG_ERR,"can't send - %m");
38335137Sphk      }
38435137Sphk    }
38535137Sphk  }
38635137Sphk}
38735137Sphk
38835734Scharnierstatic void
38935734Scharnierusage()
39035734Scharnier{
39135734Scharnier	fprintf(stderr,
39274948Sphk"usage: nos-tun -t <tun_name> -s <source_addr> -d <dest_addr> -p <protocol_number> [<source_addr>] <target_addr>\n");
39335734Scharnier	exit(1);
39435734Scharnier}
39535734Scharnier
396