nos-tun.c revision 61438
1126274Sdes/*
2124208Sdes * Copyright (c) 1996, Nickolay Dudorov
3124208Sdes * All rights reserved.
4124208Sdes *
5124208Sdes * Redistribution and use in source and binary forms, with or without
6124208Sdes * modification, are permitted provided that the following conditions
7124208Sdes * are met:
8124208Sdes * 1. Redistributions of source code must retain the above copyright
9124208Sdes *    notice unmodified, this list of conditions, and the following
10124208Sdes *    disclaimer.
11124208Sdes * 2. Redistributions in binary form must reproduce the above copyright
12124208Sdes *    notice, this list of conditions and the following disclaimer in the
13124208Sdes *    documentation and/or other materials provided with the distribution.
14124208Sdes *
15124208Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16124208Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17124208Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18124208Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19124208Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20124208Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21124208Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22124208Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23124208Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24124208Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25124208Sdes * SUCH DAMAGE.
26124208Sdes *
27124208Sdes */
28124208Sdes
29124208Sdes/*
30124208Sdes *  'nos-tun' program configure tunN interface as a point-to-point
31124208Sdes *  connection with two "pseudo"-addresses between this host and
32124208Sdes *  'target'.
33124208Sdes *
34124208Sdes *  It uses Ip-over-Ip incapsulation ( protocol number 94 - IPIP)
35124208Sdes *  (known as NOS-incapsulation in CISCO-routers' terminology).
36124208Sdes *
37124208Sdes *  'nos-tun' can works with itself and CISCO-routers.
38124208Sdes *  (It may also work with Linux 'nos-tun's, but
39124208Sdes *  I have no Linux system here to test with).
40124208Sdes *
41124208Sdes *  BUGS (or features ?):
42126274Sdes *  - you must specify ONE of the target host's addresses
43124208Sdes *    ( nos-tun sends and accepts packets only to/from this
44126274Sdes *      address )
45126274Sdes *  - there can be only ONE tunnel between two hosts,
46126274Sdes *    more precisely - between given host and (one of)
47126274Sdes *    target hosts' address(es)
48126274Sdes *    (and why do you want more ?)
49124208Sdes */
50124208Sdes
51124208Sdes/*
52124208Sdes * Mar. 23 1999 by Isao SEKI <iseki@gongon.com>
53124208Sdes * I added a new flag for ip protocol number.
54124208Sdes * We are using 4 as protocol number in ampr.org.
55126274Sdes *
56124208Sdes */
57124208Sdes
58124208Sdes#ifndef lint
59124208Sdesstatic const char rcsid[] =
60124208Sdes  "$FreeBSD: head/sbin/nos-tun/nos-tun.c 61438 2000-06-09 06:45:18Z asmodai $";
61124208Sdes#endif /* not lint */
62124208Sdes
63124208Sdes#include <fcntl.h>
64124208Sdes#include <netdb.h>
65124208Sdes#include <stdio.h>
66124208Sdes#include <stdlib.h>
67124208Sdes#include <string.h>
68128456Sdes#include <syslog.h>
69124208Sdes#include <sys/signal.h>
70128456Sdes#include <sys/socket.h>
71124208Sdes#include <sys/ioctl.h>
72124208Sdes#include <netinet/in.h>
73124208Sdes#include <netinet/in_systm.h>
74124208Sdes#include <netinet/ip.h>
75124208Sdes#include <net/if.h>
76124208Sdes#include <arpa/inet.h>
77124208Sdes#include <unistd.h>
78124208Sdes
79124208Sdes/* Tunnel interface configuration stuff */
80124208Sdesstatic struct ifaliasreq ifra;
81124208Sdesstatic struct ifreq ifrq;
82124208Sdes
83124208Sdes/* Global descriptors */
84124208Sdesint net;                          /* socket descriptor */
85124208Sdesint tun;                          /* tunnel descriptor */
86124208Sdes
87124208Sdesstatic void usage __P((void));
88124208Sdes
89124208Sdesint Set_address(char *addr, struct sockaddr_in *sin)
90124208Sdes{
91124208Sdes  struct hostent *hp;
92124208Sdes
93124208Sdes  bzero((char *)sin, sizeof(struct sockaddr));
94124208Sdes  sin->sin_family = AF_INET;
95124208Sdes  if((sin->sin_addr.s_addr = inet_addr(addr)) == (u_long)-1) {
96124208Sdes    hp = gethostbyname(addr);
97124208Sdes    if (!hp) {
98124208Sdes      syslog(LOG_ERR,"unknown host %s", addr);
99124208Sdes      return 1;
100124208Sdes    }
101124208Sdes    sin->sin_family = hp->h_addrtype;
102124208Sdes    bcopy(hp->h_addr, (caddr_t)&sin->sin_addr, hp->h_length);
103124208Sdes  }
104124208Sdes  return 0;
105124208Sdes}
106124208Sdes
107124208Sdesint tun_open(char *devname, struct sockaddr *ouraddr, char *theiraddr)
108124208Sdes{
109124208Sdes  int s;
110124208Sdes  struct sockaddr_in *sin;
111124208Sdes
112124208Sdes  /* Open tun device */
113124208Sdes  tun = open (devname, O_RDWR);
114124208Sdes  if (tun < 0) {
115124208Sdes    syslog(LOG_ERR,"can't open %s - %m",devname);
116124208Sdes    return(1);
117126274Sdes  }
118124208Sdes
119124208Sdes  /*
120124208Sdes   * At first, name the interface.
121124208Sdes   */
122124208Sdes  bzero((char *)&ifra, sizeof(ifra));
123124208Sdes  bzero((char *)&ifrq, sizeof(ifrq));
124124208Sdes
125124208Sdes  strncpy(ifrq.ifr_name, devname+5, IFNAMSIZ);
126124208Sdes  strncpy(ifra.ifra_name, devname+5, IFNAMSIZ);
127124208Sdes
128124208Sdes  s = socket(AF_INET, SOCK_DGRAM, 0);
129124208Sdes  if (s < 0) {
130124208Sdes    syslog(LOG_ERR,"can't open socket - %m");
131124208Sdes    goto tunc_return;
132124208Sdes  }
133124208Sdes
134124208Sdes  /*
135124208Sdes   *  Delete (previous) adresses for interface
136124208Sdes   *
137126274Sdes   *  !!!!
138126274Sdes   *  On FreeBSD this ioctl returns error
139124208Sdes   *  when tunN have no addresses, so - log and ignore it.
140126274Sdes   *
141124208Sdes   */
142124208Sdes  if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
143124208Sdes    syslog(LOG_ERR,"SIOCDIFADDR - %m");
144124208Sdes  }
145124208Sdes
146124208Sdes  /*
147124208Sdes   *  Set interface address
148124208Sdes   */
149124208Sdes  sin = (struct sockaddr_in *)&(ifra.ifra_addr);
150124208Sdes  bcopy(ouraddr, sin, sizeof(struct sockaddr_in));
151124208Sdes  sin->sin_len = sizeof(*sin);
152124208Sdes
153124208Sdes  /*
154124208Sdes   *  Set destination address
155124208Sdes   */
156124208Sdes  sin = (struct sockaddr_in *)&(ifra.ifra_broadaddr);
157124208Sdes  if(Set_address(theiraddr,sin)) {
158124208Sdes    syslog(LOG_ERR,"bad destination address: %s",theiraddr);
159124208Sdes    goto stunc_return;
160124208Sdes  }
161126274Sdes  sin->sin_len = sizeof(*sin);
162124208Sdes
163124208Sdes  if (ioctl(s, SIOCAIFADDR, &ifra) < 0) {
164124208Sdes    syslog(LOG_ERR,"can't set interface address - %m");
165124208Sdes    goto stunc_return;
166124208Sdes  }
167124208Sdes
168124208Sdes  /*
169124208Sdes   *  Now, bring up the interface.
170124208Sdes   */
171124208Sdes  if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
172124208Sdes    syslog(LOG_ERR,"can't get interface flags - %m");
173124208Sdes    goto stunc_return;
174124208Sdes  }
175124208Sdes
176124208Sdes  ifrq.ifr_flags |= IFF_UP;
177124208Sdes  if (!(ioctl(s, SIOCSIFFLAGS, &ifrq) < 0)) {
178124208Sdes    close(s);
179126274Sdes    return(0);
180124208Sdes  }
181124208Sdes  syslog(LOG_ERR,"can't set interface UP - %m");
182124208Sdesstunc_return:
183124208Sdes  close(s);
184124208Sdestunc_return:
185124208Sdes  close(tun);
186124208Sdes  return(1);
187124208Sdes}
188126274Sdes
189126274Sdesvoid Finish(int signum)
190126274Sdes{
191124208Sdes  int s;
192124208Sdes
193124208Sdes  syslog(LOG_INFO,"exiting");
194126274Sdes
195124208Sdes  close(net);
196124208Sdes
197124208Sdes  s = socket(AF_INET, SOCK_DGRAM, 0);
198124208Sdes  if (s < 0) {
199124208Sdes    syslog(LOG_ERR,"can't open socket - %m");
200124208Sdes    goto closing_tun;
201124208Sdes  }
202124208Sdes
203124208Sdes  /*
204124208Sdes   *  Shut down interface.
205124208Sdes   */
206124208Sdes  if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
207124208Sdes    syslog(LOG_ERR,"can't get interface flags - %m");
208124208Sdes    goto closing_fds;
209124208Sdes  }
210124208Sdes
211124208Sdes  ifrq.ifr_flags &= ~(IFF_UP|IFF_RUNNING);
212124208Sdes  if (ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
213124208Sdes    syslog(LOG_ERR,"can't set interface DOWN - %m");
214124208Sdes    goto closing_fds;
215  }
216
217  /*
218   *  Delete adresses for interface
219   */
220  bzero(&ifra.ifra_addr, sizeof(ifra.ifra_addr));
221  bzero(&ifra.ifra_broadaddr, sizeof(ifra.ifra_addr));
222  bzero(&ifra.ifra_mask, sizeof(ifra.ifra_addr));
223  if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
224    syslog(LOG_ERR,"can't delete interface's addresses - %m");
225  }
226closing_fds:
227  close(s);
228closing_tun:
229  close(tun);
230  closelog();
231  exit(signum);
232}
233
234int main (int argc, char **argv)
235{
236  int  c, len, ipoff;
237
238  char *devname = NULL;
239  char *point_to = NULL;
240  char *to_point = NULL;
241  char *target;
242  char *protocol = NULL;
243  int protnum;
244
245  struct sockaddr t_laddr;          /* Source address of tunnel */
246  struct sockaddr whereto;          /* Destination of tunnel */
247  struct sockaddr_in *to;
248
249  char buf[0x2000];                 /* Packets buffer */
250  struct ip *ip = (struct ip *)buf;
251
252  fd_set rfds, wfds, efds;          /* File descriptors for select() */
253  int nfds;                         /* Return from select() */
254
255
256  while ((c = getopt(argc, argv, "d:s:t:p:")) != -1) {
257    switch (c) {
258    case 'd':
259      to_point = optarg;
260      break;
261    case 's':
262      point_to = optarg;
263      break;
264    case 't':
265      devname = optarg;
266      break;
267    case 'p':
268      protocol = optarg;
269      break;
270    }
271  }
272  argc -= optind;
273  argv += optind;
274
275  if (argc != 1 || (devname == NULL) ||
276      (point_to == NULL) || (to_point == NULL)) {
277    usage();
278  }
279
280  if(protocol == NULL)
281      protnum = 94;
282  else
283      protnum = atoi(protocol);
284
285  target = *argv;
286
287  /* Establish logging through 'syslog' */
288  openlog("nos-tun", LOG_PID, LOG_DAEMON);
289
290  if(Set_address(point_to, (struct sockaddr_in *)&t_laddr)) {
291    closelog();
292    exit(2);
293  }
294
295  if(tun_open(devname, &t_laddr, to_point)) {
296    closelog();
297    exit(3);
298  }
299
300  to = (struct sockaddr_in *)&whereto;
301  if(Set_address(target, to))
302    Finish(4);
303
304  if ((net = socket(AF_INET, SOCK_RAW, protnum)) < 0) {
305    syslog(LOG_ERR,"can't open socket - %m");
306    Finish(5);
307  }
308
309  if (connect(net,&whereto,sizeof(struct sockaddr_in)) < 0 ) {
310    syslog(LOG_ERR,"can't connect to target - %m");
311    close(net);
312    Finish(6);
313  }
314
315  /*  Demonize it */
316  daemon(0,0);
317
318  /* Install signal handlers */
319  (void)signal(SIGHUP,Finish);
320  (void)signal(SIGINT,Finish);
321  (void)signal(SIGTERM,Finish);
322
323  for (;;) {
324    /* Set file descriptors for select() */
325    FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds);
326    FD_SET(tun,&rfds); FD_SET(net,&rfds);
327
328    nfds = select(net+10,&rfds,&wfds,&efds,NULL);
329    if(nfds < 0) {
330      syslog(LOG_ERR,"interrupted select");
331      close(net);
332      Finish(7);
333    }
334    if(nfds == 0) {         /* Impossible ? */
335      syslog(LOG_ERR,"timeout in select");
336      close(net);
337      Finish(8);
338    }
339
340
341    if(FD_ISSET(net,&rfds)) {
342      /* Read from socket ... */
343      len = read(net, buf, sizeof(buf));
344      /* Check if this is "our" packet */
345      if((ip->ip_src).s_addr == (to->sin_addr).s_addr) {
346	/* ... skip encapsulation headers ... */
347	ipoff = (ip->ip_hl << 2);
348	/* ... and write to tun-device */
349	write(tun,buf+ipoff,len-ipoff);
350      }
351    }
352
353    if(FD_ISSET(tun,&rfds)) {
354      /* Read from tun ... */
355      len = read(tun, buf, sizeof(buf));
356      /* ... and send to network */
357      if(send(net, buf, len,0) <= 0) {
358	syslog(LOG_ERR,"can't send - %m");
359      }
360    }
361  }
362}
363
364static void
365usage()
366{
367	fprintf(stderr,
368"usage: nos-tun -t <tun_name> -s <source_addr> -d <dest_addr> -p <protocol_number> <target_addr>\n");
369	exit(1);
370}
371
372