Make sure ioctl doesn't get swallowed (fit in 8 bits).
[dragonfly.git] / sbin / nos-tun / nos-tun.c
blob4efc19aa6bbe6a2ffbdf2fffb279f11f48397144
1 /*
2 * Copyright (c) 1996, Nickolay Dudorov
3 * All rights reserved.
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.
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.
27 * $FreeBSD: src/sbin/nos-tun/nos-tun.c,v 1.6.2.2 2001/08/01 23:14:00 obrien Exp $
28 * $DragonFly: src/sbin/nos-tun/nos-tun.c,v 1.6 2005/04/18 20:17:58 joerg Exp $
32 * 'nos-tun' program configure tunN interface as a point-to-point
33 * connection with two "pseudo"-addresses between this host and
34 * 'target'.
36 * It uses Ip-over-Ip incapsulation ( protocol number 94 - IPIP)
37 * (known as NOS-incapsulation in CISCO-routers' terminology).
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).
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 ?)
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.
60 #include <fcntl.h>
61 #include <netdb.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <syslog.h>
66 #include <sys/signal.h>
67 #include <sys/socket.h>
68 #include <sys/ioctl.h>
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
72 #include <net/if.h>
73 #include <arpa/inet.h>
74 #include <unistd.h>
76 /* Tunnel interface configuration stuff */
77 static struct ifaliasreq ifra;
78 static struct ifreq ifrq;
80 /* Global descriptors */
81 int net; /* socket descriptor */
82 int tun; /* tunnel descriptor */
84 static void usage(void);
86 static int
87 Set_address(char *addr, struct sockaddr_in *my_sin)
89 struct hostent *hp;
91 bzero(my_sin, sizeof(struct sockaddr));
92 my_sin->sin_family = AF_INET;
93 if((my_sin->sin_addr.s_addr = inet_addr(addr)) == (in_addr_t)-1) {
94 hp = gethostbyname(addr);
95 if (!hp) {
96 syslog(LOG_ERR,"unknown host %s", addr);
97 return 1;
99 my_sin->sin_family = hp->h_addrtype;
100 bcopy(hp->h_addr, (caddr_t)&my_sin->sin_addr, hp->h_length);
102 return 0;
105 static int
106 tun_open(char *dev_name, struct sockaddr *ouraddr, char *theiraddr)
108 int s;
109 struct sockaddr_in *my_sin;
111 /* Open tun device */
112 tun = open (dev_name, O_RDWR);
113 if (tun < 0) {
114 syslog(LOG_ERR,"can't open %s - %m",dev_name);
115 return(1);
119 * At first, name the interface.
121 bzero((char *)&ifra, sizeof(ifra));
122 bzero((char *)&ifrq, sizeof(ifrq));
124 strncpy(ifrq.ifr_name, dev_name + 5, IFNAMSIZ);
125 strncpy(ifra.ifra_name, dev_name + 5, IFNAMSIZ);
127 s = socket(AF_INET, SOCK_DGRAM, 0);
128 if (s < 0) {
129 syslog(LOG_ERR,"can't open socket - %m");
130 goto tunc_return;
134 * Delete (previous) addresses for interface
136 * !!!!
137 * On FreeBSD this ioctl returns error
138 * when tunN have no addresses, so - log and ignore it.
141 if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
142 syslog(LOG_ERR,"SIOCDIFADDR - %m");
146 * Set interface address
148 my_sin = (struct sockaddr_in *)&(ifra.ifra_addr);
149 bcopy(ouraddr, my_sin, sizeof(struct sockaddr_in));
150 my_sin->sin_len = sizeof(*my_sin);
153 * Set destination address
155 my_sin = (struct sockaddr_in *)&(ifra.ifra_broadaddr);
156 if (Set_address(theiraddr, my_sin)) {
157 syslog(LOG_ERR,"bad destination address: %s",theiraddr);
158 goto stunc_return;
160 my_sin->sin_len = sizeof(*my_sin);
162 if (ioctl(s, SIOCAIFADDR, &ifra) < 0) {
163 syslog(LOG_ERR,"can't set interface address - %m");
164 goto stunc_return;
168 * Now, bring up the interface.
170 if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
171 syslog(LOG_ERR,"can't get interface flags - %m");
172 goto stunc_return;
175 ifrq.ifr_flags |= IFF_UP;
176 if (!(ioctl(s, SIOCSIFFLAGS, &ifrq) < 0)) {
177 close(s);
178 return(0);
180 syslog(LOG_ERR,"can't set interface UP - %m");
181 stunc_return:
182 close(s);
183 tunc_return:
184 close(tun);
185 return(1);
188 static void
189 Finish(int signum)
191 int s;
193 syslog(LOG_INFO,"exiting");
195 close(net);
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;
204 * Shut down interface.
206 if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
207 syslog(LOG_ERR,"can't get interface flags - %m");
208 goto closing_fds;
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;
218 * Delete addresses for interface
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");
226 closing_fds:
227 close(s);
228 closing_tun:
229 close(tun);
230 closelog();
231 exit(signum);
234 int main (int argc, char **argv)
236 int c, len, ipoff;
238 char *dev_name = NULL;
239 char *point_to = NULL;
240 char *to_point = NULL;
241 char *target;
242 char *protocol = NULL;
243 int protnum;
245 struct sockaddr t_laddr; /* Source address of tunnel */
246 struct sockaddr whereto; /* Destination of tunnel */
247 struct sockaddr_in *to;
249 char buf[0x2000]; /* Packets buffer */
250 struct ip *ip = (struct ip *)buf;
252 fd_set rfds, wfds, efds; /* File descriptors for select() */
253 int nfds; /* Return from select() */
256 while ((c = getopt(argc, argv, "d:s:t:p:")) != -1) {
257 switch (c) {
258 case 'd':
259 to_point = optarg;
260 break;
261 case 's':
262 point_to = optarg;
263 break;
264 case 't':
265 dev_name = optarg;
266 break;
267 case 'p':
268 protocol = optarg;
269 break;
272 argc -= optind;
273 argv += optind;
275 if (argc != 1 || (dev_name == NULL) ||
276 (point_to == NULL) || (to_point == NULL)) {
277 usage();
280 if(protocol == NULL)
281 protnum = 94;
282 else
283 protnum = atoi(protocol);
285 target = *argv;
287 /* Establish logging through 'syslog' */
288 openlog("nos-tun", LOG_PID, LOG_DAEMON);
290 if(Set_address(point_to, (struct sockaddr_in *)&t_laddr)) {
291 closelog();
292 exit(2);
295 if(tun_open(dev_name, &t_laddr, to_point)) {
296 closelog();
297 exit(3);
300 to = (struct sockaddr_in *)&whereto;
301 if(Set_address(target, to))
302 Finish(4);
304 if ((net = socket(AF_INET, SOCK_RAW, protnum)) < 0) {
305 syslog(LOG_ERR,"can't open socket - %m");
306 Finish(5);
309 if (connect(net,&whereto,sizeof(struct sockaddr_in)) < 0 ) {
310 syslog(LOG_ERR,"can't connect to target - %m");
311 close(net);
312 Finish(6);
315 /* Demonize it */
316 daemon(0,0);
318 /* Install signal handlers */
319 signal(SIGHUP,Finish);
320 signal(SIGINT,Finish);
321 signal(SIGTERM,Finish);
323 for (;;) {
324 /* Set file descriptors for select() */
325 FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds);
326 FD_SET(tun,&rfds); FD_SET(net,&rfds);
328 nfds = select(net+10,&rfds,&wfds,&efds,NULL);
329 if(nfds < 0) {
330 syslog(LOG_ERR,"interrupted select");
331 close(net);
332 Finish(7);
334 if(nfds == 0) { /* Impossible ? */
335 syslog(LOG_ERR,"timeout in select");
336 close(net);
337 Finish(8);
341 if(FD_ISSET(net,&rfds)) {
342 /* Read from socket ... */
343 len = read(net, buf, sizeof(buf));
344 /* Check if this is "our" packet */
345 if((ip->ip_src).s_addr == (to->sin_addr).s_addr) {
346 /* ... skip encapsulation headers ... */
347 ipoff = (ip->ip_hl << 2);
348 /* ... and write to tun-device */
349 write(tun,buf+ipoff,len-ipoff);
353 if(FD_ISSET(tun,&rfds)) {
354 /* Read from tun ... */
355 len = read(tun, buf, sizeof(buf));
356 /* ... and send to network */
357 if(send(net, buf, len,0) <= 0) {
358 syslog(LOG_ERR,"can't send - %m");
364 static void
365 usage(void)
367 fprintf(stderr,
368 "usage: nos-tun -t <tun_name> -s <source_addr> -d <dest_addr> -p <protocol_number> <target_addr>\n");
369 exit(1);