nos-tun.c revision 35137
1/*
2 * Copyright (c) 1996, Nickolay Dudorov
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29/*
30 *  'nos_tun' program configure tunN interface as a point-to-point
31 *  connection with two "pseudo"-addresses between this host and
32 *  'target'.
33 *
34 *  It uses Ip-over-Ip incapsulation ( protocol number 94 - IPIP)
35 *  (known as NOS-incapsulation in CISCO-routers' terminology).
36 *
37 *  'nos_tun' can works with itself and CISCO-routers.
38 *  (It may also work with Linux 'nos_tun's, but
39 *  I have no Linux system here to test with).
40 *
41 *  BUGS (or features ?):
42 *  - you must specify ONE of the target host's addresses
43 *    ( nos_tun sends and accepts packets only to/from this
44 *      address )
45 *  - there can be only ONE tunnel between two hosts,
46 *    more precisely - between given host and (one of)
47 *    target hosts' address(es)
48 *    (and why do you want more ?)
49 */
50
51
52#include <stdio.h>
53#include <stdlib.h>
54#include <unistd.h>
55#include <string.h>
56#include <fcntl.h>
57#include <netdb.h>
58#include <syslog.h>
59#include <sys/param.h>
60#include <sys/socket.h>
61#include <sys/ioctl.h>
62#include <sys/signal.h>
63#include <sys/time.h>
64#include <netinet/in.h>
65#include <netinet/in_systm.h>
66#include <netinet/ip.h>
67#include <net/if.h>
68#include <arpa/inet.h>
69
70
71/* Tunnel interface configuration stuff */
72static struct ifaliasreq ifra;
73static struct ifreq ifrq;
74
75/* Global descriptors */
76int net;                          /* socket descriptor */
77int tun;                          /* tunnel descriptor */
78
79int Set_address(char *addr, struct sockaddr_in *sin)
80{
81  struct hostent *hp;
82
83  bzero((char *)sin, sizeof(struct sockaddr));
84  sin->sin_family = AF_INET;
85  if((sin->sin_addr.s_addr = inet_addr(addr)) == (u_long)-1) {
86    hp = gethostbyname(addr);
87    if (!hp) {
88      syslog(LOG_ERR,"Unknown host %s\n", addr);
89      return 1;
90    }
91    sin->sin_family = hp->h_addrtype;
92    bcopy(hp->h_addr, (caddr_t)&sin->sin_addr, hp->h_length);
93  }
94  return 0;
95}
96
97int tun_open(char *devname, struct sockaddr *ouraddr, char *theiraddr)
98{
99  int s;
100  struct sockaddr_in *sin;
101
102  /* Open tun device */
103  tun = open (devname, O_RDWR);
104  if (tun < 0) {
105    syslog(LOG_ERR,"Can't open %s - %m",devname);
106    return(1);
107  }
108
109  /*
110   * At first, name the interface.
111   */
112  bzero((char *)&ifra, sizeof(ifra));
113  bzero((char *)&ifrq, sizeof(ifrq));
114
115  strncpy(ifrq.ifr_name, devname+5, IFNAMSIZ);
116  strncpy(ifra.ifra_name, devname+5, IFNAMSIZ);
117
118  s = socket(AF_INET, SOCK_DGRAM, 0);
119  if (s < 0) {
120    syslog(LOG_ERR,"Can't open socket - %M");
121    goto tunc_return;
122  }
123
124  /*
125   *  Delete (previous) adresses for interface
126   *
127   *  !!!!
128   *  On FreeBSD this ioctl returns error
129   *  when tunN have no addresses, so - log and ignore it.
130   *
131   */
132  if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
133    syslog(LOG_ERR,"SIOCDIFADDR - %m");
134  }
135
136  /*
137   *  Set interface address
138   */
139  sin = (struct sockaddr_in *)&(ifra.ifra_addr);
140  bcopy(ouraddr, sin, sizeof(struct sockaddr_in));
141  sin->sin_len = sizeof(*sin);
142
143  /*
144   *  Set destination address
145   */
146  sin = (struct sockaddr_in *)&(ifra.ifra_broadaddr);
147  if(Set_address(theiraddr,sin)) {
148    syslog(LOG_ERR,"Bad destination address:%s\n",theiraddr);
149    goto stunc_return;
150  }
151  sin->sin_len = sizeof(*sin);
152
153  if (ioctl(s, SIOCAIFADDR, &ifra) < 0) {
154    syslog(LOG_ERR,"Can't set interface address - %m");
155    goto stunc_return;
156  }
157
158  /*
159   *  Now, bring up the interface.
160   */
161  if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
162    syslog(LOG_ERR,"Can't get interface flags - %m");
163    goto stunc_return;
164  }
165
166  ifrq.ifr_flags |= IFF_UP;
167  if (!(ioctl(s, SIOCSIFFLAGS, &ifrq) < 0)) {
168    close(s);
169    return(0);
170  }
171  syslog(LOG_ERR,"Can't set interface UP - %m");
172stunc_return:
173  close(s);
174tunc_return:
175  close(tun);
176  return(1);
177}
178
179void Finish(int signum)
180{
181  int s;
182
183  syslog(LOG_INFO,"Exiting");
184
185  close(net);
186
187  s = socket(AF_INET, SOCK_DGRAM, 0);
188  if (s < 0) {
189    syslog(LOG_ERR,"Can't open socket - %m");
190    goto closing_tun;
191  }
192
193  /*
194   *  Shut down interface.
195   */
196  if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
197    syslog(LOG_ERR,"Can't get interface flags - %m");
198    goto closing_fds;
199  }
200
201  ifrq.ifr_flags &= ~(IFF_UP|IFF_RUNNING);
202  if (ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
203    syslog(LOG_ERR,"Can't set interface DOWN - %m");
204    goto closing_fds;
205  }
206
207  /*
208   *  Delete adresses for interface
209   */
210  bzero(&ifra.ifra_addr, sizeof(ifra.ifra_addr));
211  bzero(&ifra.ifra_broadaddr, sizeof(ifra.ifra_addr));
212  bzero(&ifra.ifra_mask, sizeof(ifra.ifra_addr));
213  if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
214    syslog(LOG_ERR,"Can't delete interface's addresses - %m");
215  }
216closing_fds:
217  close(s);
218closing_tun:
219  close(tun);
220  closelog();
221  exit(signum);
222}
223
224int main (int argc, char **argv)
225{
226  int  c, len, ipoff;
227
228  char *devname = NULL;
229  char *point_to = NULL;
230  char *to_point = NULL;
231  char *target;
232
233  struct sockaddr t_laddr;          /* Source address of tunnel */
234  struct sockaddr whereto;          /* Destination of tunnel */
235  struct sockaddr_in *to;
236
237  char buf[0x2000];                 /* Packets buffer */
238  struct ip *ip = (struct ip *)buf;
239
240  fd_set rfds, wfds, efds;          /* File descriptors for select() */
241  int nfds;                         /* Return from select() */
242
243
244  while ((c = getopt(argc, argv, "d:s:t:")) != EOF) {
245    switch (c) {
246    case 'd':
247      to_point = optarg;
248      break;
249    case 's':
250      point_to = optarg;
251      break;
252    case 't':
253      devname = optarg;
254      break;
255    }
256  }
257  argc -= optind;
258  argv += optind;
259
260  if (argc != 1 || (devname == NULL) ||
261      (point_to == NULL) || (to_point == NULL)) {
262    fprintf(stderr,
263      "Usage: nos_tun -t <tun_name> -s <source_addr> -d <dest_addr> <target_addr>\n");
264    exit(1);
265  }
266
267  target = *argv;
268
269  /* Establish logging through 'syslog' */
270  openlog("nos_tun", LOG_PID, LOG_DAEMON);
271
272  if(Set_address(point_to, (struct sockaddr_in *)&t_laddr)) {
273    closelog();
274    exit(2);
275  }
276
277  if(tun_open(devname, &t_laddr, to_point)) {
278    closelog();
279    exit(3);
280  }
281
282  to = (struct sockaddr_in *)&whereto;
283  if(Set_address(target, to))
284    Finish(4);
285
286  if ((net = socket(AF_INET, SOCK_RAW, 94)) < 0) {
287    syslog(LOG_ERR,"Can't open socket - %m");
288    Finish(5);
289  }
290
291  if (connect(net,&whereto,sizeof(struct sockaddr_in)) < 0 ) {
292    syslog(LOG_ERR,"Can't connect to target - %m");
293    close(net);
294    Finish(6);
295  }
296
297  /*  Demonize it */
298  daemon(0,0);
299
300  /* Install signal handlers */
301  (void)signal(SIGHUP,Finish);
302  (void)signal(SIGINT,Finish);
303  (void)signal(SIGTERM,Finish);
304
305  for (;;) {
306    /* Set file descriptors for select() */
307    FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds);
308    FD_SET(tun,&rfds); FD_SET(net,&rfds);
309
310    nfds = select(net+10,&rfds,&wfds,&efds,NULL);
311    if(nfds < 0) {
312      syslog(LOG_ERR,"Interrupted select");
313      close(net);
314      Finish(7);
315    }
316    if(nfds == 0) {         /* Impossible ? */
317      syslog(LOG_ERR,"Timeout in select");
318      close(net);
319      Finish(8);
320    }
321
322
323    if(FD_ISSET(net,&rfds)) {
324      /* Read from socket ... */
325      len = read(net, buf, sizeof(buf));
326      /* Check if this is "our" packet */
327      if((ip->ip_src).s_addr == (to->sin_addr).s_addr) {
328	/* ... skip encapsulation headers ... */
329	ipoff = (ip->ip_hl << 2);
330	/* ... and write to tun-device */
331	write(tun,buf+ipoff,len-ipoff);
332      }
333    }
334
335    if(FD_ISSET(tun,&rfds)) {
336      /* Read from tun ... */
337      len = read(tun, buf, sizeof(buf));
338      /* ... and send to network */
339      if(send(net, buf, len,0) <= 0) {
340	syslog(LOG_ERR,"Can't send - %m");
341      }
342    }
343  }
344}
345
346