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