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