xref: /dragonfly/sbin/nos-tun/nos-tun.c (revision 37de577a)
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  * $FreeBSD: src/sbin/nos-tun/nos-tun.c,v 1.6.2.2 2001/08/01 23:14:00 obrien Exp $
28  */
29 
30 /*
31  *  'nos-tun' program configure tunN interface as a point-to-point
32  *  connection with two "pseudo"-addresses between this host and
33  *  'target'.
34  *
35  *  It uses Ip-over-Ip incapsulation ( protocol number 94 - IPIP)
36  *  (known as NOS-incapsulation in CISCO-routers' terminology).
37  *
38  *  'nos-tun' can works with itself and CISCO-routers.
39  *  (It may also work with Linux 'nos-tun's, but
40  *  I have no Linux system here to test with).
41  *
42  *  BUGS (or features ?):
43  *  - you must specify ONE of the target host's addresses
44  *    ( nos-tun sends and accepts packets only to/from this
45  *      address )
46  *  - there can be only ONE tunnel between two hosts,
47  *    more precisely - between given host and (one of)
48  *    target hosts' address(es)
49  *    (and why do you want more ?)
50  */
51 
52 /*
53  * Mar. 23 1999 by Isao SEKI <iseki@gongon.com>
54  * I added a new flag for ip protocol number.
55  * We are using 4 as protocol number in ampr.org.
56  */
57 
58 #include <fcntl.h>
59 #include <netdb.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <syslog.h>
64 #include <sys/signal.h>
65 #include <sys/socket.h>
66 #include <sys/ioctl.h>
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <net/if.h>
71 #include <net/tun/if_tun.h>
72 #include <arpa/inet.h>
73 #include <unistd.h>
74 
75 /* Tunnel interface configuration stuff */
76 static struct ifaliasreq ifra;
77 static struct ifreq ifrq;
78 
79 /* Tun(4) device autocloner */
80 static const char *tun_dev = "/dev/tun";
81 
82 /* Global descriptors */
83 int net;                          /* socket descriptor */
84 int tun;                          /* tunnel descriptor */
85 
86 static void usage(void);
87 
88 static int
89 Set_address(char *addr, struct sockaddr_in *my_sin)
90 {
91   struct hostent *hp;
92 
93   bzero(my_sin, sizeof(struct sockaddr));
94   my_sin->sin_family = AF_INET;
95   if((my_sin->sin_addr.s_addr = inet_addr(addr)) == (in_addr_t)-1) {
96     hp = gethostbyname(addr);
97     if (!hp) {
98       syslog(LOG_ERR,"unknown host %s", addr);
99       return 1;
100     }
101     my_sin->sin_family = hp->h_addrtype;
102     bcopy(hp->h_addr, (caddr_t)&my_sin->sin_addr, hp->h_length);
103   }
104   return 0;
105 }
106 
107 static int
108 tun_open(struct sockaddr *ouraddr, char *theiraddr)
109 {
110   int s;
111   struct sockaddr_in *my_sin;
112 
113   /* Open tun device */
114   tun = open(tun_dev, O_RDWR);
115   if (tun < 0) {
116     syslog(LOG_ERR,"can't open %s - %m",tun_dev);
117     return(1);
118   }
119 
120   /*
121    * At first, name the interface.
122    */
123   bzero((char *)&ifra, sizeof(ifra));
124   bzero((char *)&ifrq, sizeof(ifrq));
125 
126   if (ioctl(tun, TUNGIFNAME, &ifrq) < 0) {
127     syslog(LOG_ERR,"can't get tun interface name - %m");
128     goto tunc_return;
129   }
130   strlcpy(ifra.ifra_name, ifrq.ifr_name, IFNAMSIZ);
131 
132   s = socket(AF_INET, SOCK_DGRAM, 0);
133   if (s < 0) {
134     syslog(LOG_ERR,"can't open socket - %m");
135     goto tunc_return;
136   }
137 
138   /*
139    *  Delete (previous) addresses for interface
140    *
141    *  !!!!
142    *  On FreeBSD this ioctl returns error
143    *  when tunN have no addresses, so - log and ignore it.
144    */
145   if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
146     syslog(LOG_ERR,"SIOCDIFADDR - %m");
147   }
148 
149   /*
150    *  Set interface address
151    */
152   my_sin = (struct sockaddr_in *)&(ifra.ifra_addr);
153   bcopy(ouraddr, my_sin, sizeof(struct sockaddr_in));
154   my_sin->sin_len = sizeof(*my_sin);
155 
156   /*
157    *  Set destination address
158    */
159   my_sin = (struct sockaddr_in *)&(ifra.ifra_broadaddr);
160   if (Set_address(theiraddr, my_sin)) {
161     syslog(LOG_ERR,"bad destination address: %s",theiraddr);
162     goto stunc_return;
163   }
164   my_sin->sin_len = sizeof(*my_sin);
165 
166   if (ioctl(s, SIOCAIFADDR, &ifra) < 0) {
167     syslog(LOG_ERR,"can't set interface address - %m");
168     goto stunc_return;
169   }
170 
171   /*
172    *  Now, bring up the interface.
173    */
174   if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
175     syslog(LOG_ERR,"can't get interface flags - %m");
176     goto stunc_return;
177   }
178 
179   ifrq.ifr_flags |= IFF_UP;
180   if (!(ioctl(s, SIOCSIFFLAGS, &ifrq) < 0)) {
181     close(s);
182     return(0);
183   }
184   syslog(LOG_ERR,"can't set interface UP - %m");
185 
186 stunc_return:
187   close(s);
188 tunc_return:
189   close(tun);
190   return(1);
191 }
192 
193 static void
194 Finish(int signum)
195 {
196   int s;
197 
198   syslog(LOG_INFO,"exiting");
199 
200   close(net);
201 
202   s = socket(AF_INET, SOCK_DGRAM, 0);
203   if (s < 0) {
204     syslog(LOG_ERR,"can't open socket - %m");
205     goto closing_tun;
206   }
207 
208   /*
209    *  Shut down interface.
210    */
211   if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
212     syslog(LOG_ERR,"can't get interface flags - %m");
213     goto closing_fds;
214   }
215 
216   ifrq.ifr_flags &= ~(IFF_UP|IFF_RUNNING);
217   if (ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
218     syslog(LOG_ERR,"can't set interface DOWN - %m");
219     goto closing_fds;
220   }
221 
222   /*
223    *  Delete addresses for interface
224    */
225   bzero(&ifra.ifra_addr, sizeof(ifra.ifra_addr));
226   bzero(&ifra.ifra_broadaddr, sizeof(ifra.ifra_addr));
227   bzero(&ifra.ifra_mask, sizeof(ifra.ifra_addr));
228   if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
229     syslog(LOG_ERR,"can't delete interface's addresses - %m");
230   }
231 
232 closing_fds:
233   close(s);
234 closing_tun:
235   close(tun);
236   closelog();
237   exit(signum);
238 }
239 
240 int
241 main(int argc, char **argv)
242 {
243   int  c, len, ipoff;
244 
245   char *point_to = NULL;
246   char *to_point = NULL;
247   char *target;
248   char *protocol = NULL;
249   int protnum;
250 
251   struct sockaddr t_laddr;          /* Source address of tunnel */
252   struct sockaddr whereto;          /* Destination of tunnel */
253   struct sockaddr_in *to;
254 
255   char buf[0x2000];                 /* Packets buffer */
256   struct ip *ip = (struct ip *)buf;
257 
258   fd_set rfds, wfds, efds;          /* File descriptors for select() */
259   int nfds;                         /* Return from select() */
260 
261   while ((c = getopt(argc, argv, "d:s:p:")) != -1) {
262     switch (c) {
263     case 'd':
264       to_point = optarg;
265       break;
266     case 's':
267       point_to = optarg;
268       break;
269     case 'p':
270       protocol = optarg;
271       break;
272     }
273   }
274   argc -= optind;
275   argv += optind;
276 
277   if (argc != 1 || (point_to == NULL) || (to_point == NULL))
278     usage();
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(&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   signal(SIGHUP,Finish);
320   signal(SIGINT,Finish);
321   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 
364 static void
365 usage(void)
366 {
367 	fprintf(stderr, "usage: nos-tun -s <source_addr> -d <dest_addr> "
368 		"-p <protocol_number> <target_addr>\n");
369 	exit(1);
370 }
371 
372