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