xref: /freebsd/sbin/nos-tun/nos-tun.c (revision 5773cccf19ef7b97e56c1101aa481c43149224da)
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  * Mar. 23 1999 by Isao SEKI <iseki@gongon.com>
53  * I added a new flag for ip protocol number.
54  * We are using 4 as protocol number in ampr.org.
55  *
56  */
57 
58 #ifndef lint
59 static const char rcsid[] =
60   "$FreeBSD$";
61 #endif /* not lint */
62 
63 #include <sys/types.h>
64 #include <sys/ioctl.h>
65 #include <sys/signal.h>
66 #include <sys/socket.h>
67 
68 #include <net/if.h>
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
72 
73 #include <arpa/inet.h>
74 #include <fcntl.h>
75 #include <netdb.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include <syslog.h>
80 #include <unistd.h>
81 
82 /* Tunnel interface configuration stuff */
83 static struct ifaliasreq ifra;
84 static struct ifreq ifrq;
85 
86 /* Global descriptors */
87 int net;                          /* socket descriptor */
88 int tun;                          /* tunnel descriptor */
89 
90 static void usage(void);
91 
92 int Set_address(char *addr, struct sockaddr_in *sin)
93 {
94   struct hostent *hp;
95 
96   bzero((char *)sin, sizeof(struct sockaddr));
97   sin->sin_family = AF_INET;
98   if((sin->sin_addr.s_addr = inet_addr(addr)) == (u_long)-1) {
99     hp = gethostbyname(addr);
100     if (!hp) {
101       syslog(LOG_ERR,"unknown host %s", addr);
102       return 1;
103     }
104     sin->sin_family = hp->h_addrtype;
105     bcopy(hp->h_addr, (caddr_t)&sin->sin_addr, hp->h_length);
106   }
107   return 0;
108 }
109 
110 int tun_open(char *devname, struct sockaddr *ouraddr, char *theiraddr)
111 {
112   int s;
113   struct sockaddr_in *sin;
114 
115   /* Open tun device */
116   tun = open (devname, O_RDWR);
117   if (tun < 0) {
118     syslog(LOG_ERR,"can't open %s - %m",devname);
119     return(1);
120   }
121 
122   /*
123    * At first, name the interface.
124    */
125   bzero((char *)&ifra, sizeof(ifra));
126   bzero((char *)&ifrq, sizeof(ifrq));
127 
128   strncpy(ifrq.ifr_name, devname+5, IFNAMSIZ);
129   strncpy(ifra.ifra_name, devname+5, IFNAMSIZ);
130 
131   s = socket(AF_INET, SOCK_DGRAM, 0);
132   if (s < 0) {
133     syslog(LOG_ERR,"can't open socket - %m");
134     goto tunc_return;
135   }
136 
137   /*
138    *  Delete (previous) addresses for interface
139    *
140    *  !!!!
141    *  On FreeBSD this ioctl returns error
142    *  when tunN have no addresses, so - log and ignore it.
143    *
144    */
145   if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
146     syslog(LOG_ERR,"SIOCDIFADDR - %m");
147   }
148 
149   /*
150    *  Set interface address
151    */
152   sin = (struct sockaddr_in *)&(ifra.ifra_addr);
153   bcopy(ouraddr, sin, sizeof(struct sockaddr_in));
154   sin->sin_len = sizeof(*sin);
155 
156   /*
157    *  Set destination address
158    */
159   sin = (struct sockaddr_in *)&(ifra.ifra_broadaddr);
160   if(Set_address(theiraddr,sin)) {
161     syslog(LOG_ERR,"bad destination address: %s",theiraddr);
162     goto stunc_return;
163   }
164   sin->sin_len = sizeof(*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 stunc_return:
186   close(s);
187 tunc_return:
188   close(tun);
189   return(1);
190 }
191 
192 void Finish(int signum)
193 {
194   int s;
195 
196   syslog(LOG_INFO,"exiting");
197 
198   close(net);
199 
200   s = socket(AF_INET, SOCK_DGRAM, 0);
201   if (s < 0) {
202     syslog(LOG_ERR,"can't open socket - %m");
203     goto closing_tun;
204   }
205 
206   /*
207    *  Shut down interface.
208    */
209   if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
210     syslog(LOG_ERR,"can't get interface flags - %m");
211     goto closing_fds;
212   }
213 
214   ifrq.ifr_flags &= ~(IFF_UP|IFF_RUNNING);
215   if (ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
216     syslog(LOG_ERR,"can't set interface DOWN - %m");
217     goto closing_fds;
218   }
219 
220   /*
221    *  Delete addresses for interface
222    */
223   bzero(&ifra.ifra_addr, sizeof(ifra.ifra_addr));
224   bzero(&ifra.ifra_broadaddr, sizeof(ifra.ifra_addr));
225   bzero(&ifra.ifra_mask, sizeof(ifra.ifra_addr));
226   if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
227     syslog(LOG_ERR,"can't delete interface's addresses - %m");
228   }
229 closing_fds:
230   close(s);
231 closing_tun:
232   close(tun);
233   closelog();
234   exit(signum);
235 }
236 
237 int main (int argc, char **argv)
238 {
239   int  c, len, ipoff;
240 
241   char *devname = NULL;
242   char *point_to = NULL;
243   char *to_point = NULL;
244   char *target;
245   char *source = NULL;
246   char *protocol = NULL;
247   int protnum;
248 
249   struct sockaddr t_laddr;          /* Source address of tunnel */
250   struct sockaddr whereto;          /* Destination of tunnel */
251   struct sockaddr wherefrom;        /* Source of tunnel */
252   struct sockaddr_in *to;
253 
254   char buf[0x2000];                 /* Packets buffer */
255   struct ip *ip = (struct ip *)buf;
256 
257   fd_set rfds;                      /* File descriptors for select() */
258   int nfds;                         /* Return from select() */
259   int lastfd;                       /* highest fd we care about */
260 
261 
262   while ((c = getopt(argc, argv, "d:s:t:p:")) != -1) {
263     switch (c) {
264     case 'd':
265       to_point = optarg;
266       break;
267     case 's':
268       point_to = optarg;
269       break;
270     case 't':
271       devname = optarg;
272       break;
273     case 'p':
274       protocol = optarg;
275       break;
276     }
277   }
278   argc -= optind;
279   argv += optind;
280 
281   if ((argc != 1 && argc != 2) || (devname == NULL) ||
282       (point_to == NULL) || (to_point == NULL)) {
283     usage();
284   }
285 
286   if(protocol == NULL)
287       protnum = 94;
288   else
289       protnum = atoi(protocol);
290 
291   if (argc == 1) {
292       target = *argv;
293   } else {
294       source = *argv++; target = *argv;
295   }
296 
297   /* Establish logging through 'syslog' */
298   openlog("nos-tun", LOG_PID, LOG_DAEMON);
299 
300   if(Set_address(point_to, (struct sockaddr_in *)&t_laddr)) {
301     closelog();
302     exit(2);
303   }
304 
305   if(tun_open(devname, &t_laddr, to_point)) {
306     closelog();
307     exit(3);
308   }
309 
310   to = (struct sockaddr_in *)&whereto;
311   if(Set_address(target, to))
312     Finish(4);
313 
314   if ((net = socket(AF_INET, SOCK_RAW, protnum)) < 0) {
315     syslog(LOG_ERR,"can't open socket - %m");
316     Finish(5);
317   }
318 
319   if (source) {
320 	if (Set_address(source, (struct sockaddr_in *)&wherefrom))
321 	  Finish(9);
322     if (bind(net, &wherefrom, sizeof(wherefrom)) < 0) {
323 	  syslog(LOG_ERR, "can't bind source address - %m");
324 	  Finish(10);
325 	}
326   }
327 
328   if (connect(net,&whereto,sizeof(struct sockaddr_in)) < 0 ) {
329     syslog(LOG_ERR,"can't connect to target - %m");
330     close(net);
331     Finish(6);
332   }
333 
334   /*  Demonize it */
335   daemon(0,0);
336 
337   /* Install signal handlers */
338   (void)signal(SIGHUP,Finish);
339   (void)signal(SIGINT,Finish);
340   (void)signal(SIGTERM,Finish);
341 
342   if (tun > net)
343 	lastfd = tun;
344   else
345 	lastfd = net;
346 
347   for (;;) {
348     /* Set file descriptors for select() */
349     FD_ZERO(&rfds);
350     FD_SET(tun,&rfds); FD_SET(net,&rfds);
351 
352     nfds = select(lastfd+1,&rfds,NULL,NULL,NULL);
353     if(nfds < 0) {
354       syslog(LOG_ERR,"interrupted select");
355       close(net);
356       Finish(7);
357     }
358     if(nfds == 0) {         /* Impossible ? */
359       syslog(LOG_ERR,"timeout in select");
360       close(net);
361       Finish(8);
362     }
363 
364 
365     if(FD_ISSET(net,&rfds)) {
366       /* Read from socket ... */
367       len = read(net, buf, sizeof(buf));
368       /* Check if this is "our" packet */
369       if((ip->ip_src).s_addr == (to->sin_addr).s_addr) {
370 	/* ... skip encapsulation headers ... */
371 	ipoff = (ip->ip_hl << 2);
372 	/* ... and write to tun-device */
373 	write(tun,buf+ipoff,len-ipoff);
374       }
375     }
376 
377     if(FD_ISSET(tun,&rfds)) {
378       /* Read from tun ... */
379       len = read(tun, buf, sizeof(buf));
380       /* ... and send to network */
381       if(send(net, buf, len,0) <= 0) {
382 	syslog(LOG_ERR,"can't send - %m");
383       }
384     }
385   }
386 }
387 
388 static void
389 usage()
390 {
391 	fprintf(stderr,
392 "usage: nos-tun -t <tun_name> -s <source_addr> -d <dest_addr> -p <protocol_number> [<source_addr>] <target_addr>\n");
393 	exit(1);
394 }
395 
396