xref: /freebsd/sbin/nos-tun/nos-tun.c (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
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, &ifra) < 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(&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 *dev_name = 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       dev_name = 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) || (dev_name == 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(dev_name, &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(void)
390 {
391 	fprintf(stderr,
392 "usage: nos-tun -t tunnel -s source -d destination -p protocol_number [source] target\n");
393 	exit(1);
394 }
395 
396