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$";
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
92204966Suqsstatic int
93204966SuqsSet_address(char *addr, struct sockaddr_in *sin)
9435137Sphk{
9535137Sphk  struct hostent *hp;
9635137Sphk
9735137Sphk  bzero((char *)sin, sizeof(struct sockaddr));
9835137Sphk  sin->sin_family = AF_INET;
99121540Speter  if((sin->sin_addr.s_addr = inet_addr(addr)) == (in_addr_t)-1) {
10035137Sphk    hp = gethostbyname(addr);
10135137Sphk    if (!hp) {
10237669Scharnier      syslog(LOG_ERR,"unknown host %s", addr);
10335137Sphk      return 1;
10435137Sphk    }
10535137Sphk    sin->sin_family = hp->h_addrtype;
10635137Sphk    bcopy(hp->h_addr, (caddr_t)&sin->sin_addr, hp->h_length);
10735137Sphk  }
10835137Sphk  return 0;
10935137Sphk}
11035137Sphk
111204966Suqsstatic int
112204966Suqstun_open(char *dev_name, struct sockaddr *ouraddr, char *theiraddr)
11335137Sphk{
11435137Sphk  int s;
11535137Sphk  struct sockaddr_in *sin;
11635137Sphk
11735137Sphk  /* Open tun device */
118204966Suqs  tun = open(dev_name, O_RDWR);
11935137Sphk  if (tun < 0) {
120204966Suqs    syslog(LOG_ERR,"can't open %s - %m", dev_name);
12135137Sphk    return(1);
12235137Sphk  }
12335137Sphk
12435137Sphk  /*
12535137Sphk   * At first, name the interface.
12635137Sphk   */
12735137Sphk  bzero((char *)&ifra, sizeof(ifra));
12835137Sphk  bzero((char *)&ifrq, sizeof(ifrq));
12935137Sphk
130204966Suqs  strncpy(ifrq.ifr_name, dev_name+5, IFNAMSIZ);
131204966Suqs  strncpy(ifra.ifra_name, dev_name+5, IFNAMSIZ);
13235137Sphk
13335137Sphk  s = socket(AF_INET, SOCK_DGRAM, 0);
13435137Sphk  if (s < 0) {
13538023Sbde    syslog(LOG_ERR,"can't open socket - %m");
13635137Sphk    goto tunc_return;
13735137Sphk  }
13835137Sphk
13935137Sphk  /*
14080203Skris   *  Delete (previous) addresses for interface
14135137Sphk   *
14235137Sphk   *  !!!!
14335137Sphk   *  On FreeBSD this ioctl returns error
14435137Sphk   *  when tunN have no addresses, so - log and ignore it.
14535137Sphk   *
14635137Sphk   */
14735137Sphk  if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
14835137Sphk    syslog(LOG_ERR,"SIOCDIFADDR - %m");
14935137Sphk  }
15035137Sphk
15135137Sphk  /*
15235137Sphk   *  Set interface address
15335137Sphk   */
15435137Sphk  sin = (struct sockaddr_in *)&(ifra.ifra_addr);
15535137Sphk  bcopy(ouraddr, sin, sizeof(struct sockaddr_in));
15635137Sphk  sin->sin_len = sizeof(*sin);
15735137Sphk
15835137Sphk  /*
15935137Sphk   *  Set destination address
16035137Sphk   */
16135137Sphk  sin = (struct sockaddr_in *)&(ifra.ifra_broadaddr);
16235137Sphk  if(Set_address(theiraddr,sin)) {
16337669Scharnier    syslog(LOG_ERR,"bad destination address: %s",theiraddr);
16435137Sphk    goto stunc_return;
16535137Sphk  }
16635137Sphk  sin->sin_len = sizeof(*sin);
16735137Sphk
16835137Sphk  if (ioctl(s, SIOCAIFADDR, &ifra) < 0) {
16937669Scharnier    syslog(LOG_ERR,"can't set interface address - %m");
17035137Sphk    goto stunc_return;
17135137Sphk  }
17235137Sphk
17335137Sphk  /*
17435137Sphk   *  Now, bring up the interface.
17535137Sphk   */
17635137Sphk  if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
17737669Scharnier    syslog(LOG_ERR,"can't get interface flags - %m");
17835137Sphk    goto stunc_return;
17935137Sphk  }
18035137Sphk
18135137Sphk  ifrq.ifr_flags |= IFF_UP;
18235137Sphk  if (!(ioctl(s, SIOCSIFFLAGS, &ifrq) < 0)) {
18335137Sphk    close(s);
18435137Sphk    return(0);
18535137Sphk  }
18637669Scharnier  syslog(LOG_ERR,"can't set interface UP - %m");
18735137Sphkstunc_return:
18835137Sphk  close(s);
18935137Sphktunc_return:
19035137Sphk  close(tun);
19135137Sphk  return(1);
19235137Sphk}
19335137Sphk
194204966Suqsstatic void
195204966SuqsFinish(int signum)
19635137Sphk{
19735137Sphk  int s;
19835137Sphk
19937669Scharnier  syslog(LOG_INFO,"exiting");
20035137Sphk
20135137Sphk  close(net);
20235137Sphk
20335137Sphk  s = socket(AF_INET, SOCK_DGRAM, 0);
20435137Sphk  if (s < 0) {
20537669Scharnier    syslog(LOG_ERR,"can't open socket - %m");
20635137Sphk    goto closing_tun;
20735137Sphk  }
20835137Sphk
20935137Sphk  /*
21035137Sphk   *  Shut down interface.
21135137Sphk   */
21235137Sphk  if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
21337669Scharnier    syslog(LOG_ERR,"can't get interface flags - %m");
21435137Sphk    goto closing_fds;
21535137Sphk  }
21635137Sphk
21735137Sphk  ifrq.ifr_flags &= ~(IFF_UP|IFF_RUNNING);
21835137Sphk  if (ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
21937669Scharnier    syslog(LOG_ERR,"can't set interface DOWN - %m");
22035137Sphk    goto closing_fds;
22135137Sphk  }
22235137Sphk
22335137Sphk  /*
22480203Skris   *  Delete addresses for interface
22535137Sphk   */
22635137Sphk  bzero(&ifra.ifra_addr, sizeof(ifra.ifra_addr));
22735137Sphk  bzero(&ifra.ifra_broadaddr, sizeof(ifra.ifra_addr));
22835137Sphk  bzero(&ifra.ifra_mask, sizeof(ifra.ifra_addr));
22935137Sphk  if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
23037669Scharnier    syslog(LOG_ERR,"can't delete interface's addresses - %m");
23135137Sphk  }
23235137Sphkclosing_fds:
23335137Sphk  close(s);
23435137Sphkclosing_tun:
23535137Sphk  close(tun);
23635137Sphk  closelog();
23735137Sphk  exit(signum);
23835137Sphk}
23935137Sphk
24035137Sphkint main (int argc, char **argv)
24135137Sphk{
24235137Sphk  int  c, len, ipoff;
24335137Sphk
244204966Suqs  char *dev_name = NULL;
24535137Sphk  char *point_to = NULL;
24635137Sphk  char *to_point = NULL;
24735137Sphk  char *target;
24874948Sphk  char *source = NULL;
24945114Sphk  char *protocol = NULL;
25045114Sphk  int protnum;
25135137Sphk
25235137Sphk  struct sockaddr t_laddr;          /* Source address of tunnel */
25335137Sphk  struct sockaddr whereto;          /* Destination of tunnel */
25474948Sphk  struct sockaddr wherefrom;        /* Source of tunnel */
25535137Sphk  struct sockaddr_in *to;
25635137Sphk
25735137Sphk  char buf[0x2000];                 /* Packets buffer */
25835137Sphk  struct ip *ip = (struct ip *)buf;
25935137Sphk
26089583Sbillf  fd_set rfds;                      /* File descriptors for select() */
26135137Sphk  int nfds;                         /* Return from select() */
26289583Sbillf  int lastfd;                       /* highest fd we care about */
26335137Sphk
26435137Sphk
26545114Sphk  while ((c = getopt(argc, argv, "d:s:t:p:")) != -1) {
26635137Sphk    switch (c) {
26735137Sphk    case 'd':
26835137Sphk      to_point = optarg;
26935137Sphk      break;
27035137Sphk    case 's':
27135137Sphk      point_to = optarg;
27235137Sphk      break;
27335137Sphk    case 't':
274204966Suqs      dev_name = optarg;
27535137Sphk      break;
27645114Sphk    case 'p':
27745114Sphk      protocol = optarg;
27845114Sphk      break;
27935137Sphk    }
28035137Sphk  }
28135137Sphk  argc -= optind;
28235137Sphk  argv += optind;
28335137Sphk
284204966Suqs  if ((argc != 1 && argc != 2) || (dev_name == NULL) ||
28535137Sphk      (point_to == NULL) || (to_point == NULL)) {
28635734Scharnier    usage();
28735137Sphk  }
28835137Sphk
28945114Sphk  if(protocol == NULL)
29045114Sphk      protnum = 94;
29145114Sphk  else
29245114Sphk      protnum = atoi(protocol);
29345114Sphk
29474948Sphk  if (argc == 1) {
29574948Sphk      target = *argv;
29674948Sphk  } else {
29774948Sphk      source = *argv++; target = *argv;
29874948Sphk  }
29935137Sphk
30035137Sphk  /* Establish logging through 'syslog' */
30161438Sasmodai  openlog("nos-tun", LOG_PID, LOG_DAEMON);
30235137Sphk
30335137Sphk  if(Set_address(point_to, (struct sockaddr_in *)&t_laddr)) {
30435137Sphk    closelog();
30535137Sphk    exit(2);
30635137Sphk  }
30735137Sphk
308204966Suqs  if(tun_open(dev_name, &t_laddr, to_point)) {
30935137Sphk    closelog();
31035137Sphk    exit(3);
31135137Sphk  }
31235137Sphk
31335137Sphk  to = (struct sockaddr_in *)&whereto;
31435137Sphk  if(Set_address(target, to))
31535137Sphk    Finish(4);
31635137Sphk
31745114Sphk  if ((net = socket(AF_INET, SOCK_RAW, protnum)) < 0) {
31837669Scharnier    syslog(LOG_ERR,"can't open socket - %m");
31935137Sphk    Finish(5);
32035137Sphk  }
32135137Sphk
32274948Sphk  if (source) {
32374948Sphk	if (Set_address(source, (struct sockaddr_in *)&wherefrom))
32474948Sphk	  Finish(9);
32574948Sphk    if (bind(net, &wherefrom, sizeof(wherefrom)) < 0) {
32674948Sphk	  syslog(LOG_ERR, "can't bind source address - %m");
32774948Sphk	  Finish(10);
32874948Sphk	}
32974948Sphk  }
33074948Sphk
33135137Sphk  if (connect(net,&whereto,sizeof(struct sockaddr_in)) < 0 ) {
33237669Scharnier    syslog(LOG_ERR,"can't connect to target - %m");
33335137Sphk    close(net);
33435137Sphk    Finish(6);
33535137Sphk  }
33635137Sphk
33735137Sphk  /*  Demonize it */
33835137Sphk  daemon(0,0);
33935137Sphk
34035137Sphk  /* Install signal handlers */
34135137Sphk  (void)signal(SIGHUP,Finish);
34235137Sphk  (void)signal(SIGINT,Finish);
34335137Sphk  (void)signal(SIGTERM,Finish);
34435137Sphk
34589583Sbillf  if (tun > net)
34689583Sbillf	lastfd = tun;
34789583Sbillf  else
34889583Sbillf	lastfd = net;
34989583Sbillf
35035137Sphk  for (;;) {
35135137Sphk    /* Set file descriptors for select() */
35289583Sbillf    FD_ZERO(&rfds);
35335137Sphk    FD_SET(tun,&rfds); FD_SET(net,&rfds);
35435137Sphk
35589583Sbillf    nfds = select(lastfd+1,&rfds,NULL,NULL,NULL);
35635137Sphk    if(nfds < 0) {
35737669Scharnier      syslog(LOG_ERR,"interrupted select");
35835137Sphk      close(net);
35935137Sphk      Finish(7);
36035137Sphk    }
36135137Sphk    if(nfds == 0) {         /* Impossible ? */
36237669Scharnier      syslog(LOG_ERR,"timeout in select");
36335137Sphk      close(net);
36435137Sphk      Finish(8);
36535137Sphk    }
36635137Sphk
36735137Sphk
36835137Sphk    if(FD_ISSET(net,&rfds)) {
36935137Sphk      /* Read from socket ... */
37035137Sphk      len = read(net, buf, sizeof(buf));
37135137Sphk      /* Check if this is "our" packet */
37235137Sphk      if((ip->ip_src).s_addr == (to->sin_addr).s_addr) {
37335137Sphk	/* ... skip encapsulation headers ... */
37435137Sphk	ipoff = (ip->ip_hl << 2);
37535137Sphk	/* ... and write to tun-device */
37635137Sphk	write(tun,buf+ipoff,len-ipoff);
37735137Sphk      }
37835137Sphk    }
37935137Sphk
38035137Sphk    if(FD_ISSET(tun,&rfds)) {
38135137Sphk      /* Read from tun ... */
38235137Sphk      len = read(tun, buf, sizeof(buf));
38335137Sphk      /* ... and send to network */
38435137Sphk      if(send(net, buf, len,0) <= 0) {
38537669Scharnier	syslog(LOG_ERR,"can't send - %m");
38635137Sphk      }
38735137Sphk    }
38835137Sphk  }
38935137Sphk}
39035137Sphk
39135734Scharnierstatic void
392204966Suqsusage(void)
39335734Scharnier{
39435734Scharnier	fprintf(stderr,
395141611Sru"usage: nos-tun -t tunnel -s source -d destination -p protocol_number [source] target\n");
39635734Scharnier	exit(1);
39735734Scharnier}
39835734Scharnier
399