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