2 * Copyright (c) 1996, Nickolay Dudorov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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
30 * 'nos-tun' program configure tunN interface as a point-to-point
31 * connection with two "pseudo"-addresses between this host and
34 * It uses Ip-over-Ip incapsulation ( protocol number 94 - IPIP)
35 * (known as NOS-incapsulation in CISCO-routers' terminology).
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).
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
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 ?)
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.
59 static const char rcsid[] =
63 #include <sys/types.h>
64 #include <sys/ioctl.h>
65 #include <sys/signal.h>
66 #include <sys/socket.h>
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
73 #include <arpa/inet.h>
82 /* Tunnel interface configuration stuff */
83 static struct ifaliasreq ifra;
84 static struct ifreq ifrq;
86 /* Global descriptors */
87 int net; /* socket descriptor */
88 int tun; /* tunnel descriptor */
90 static void usage(void);
92 int Set_address(char *addr, struct sockaddr_in *sin)
96 bzero((char *)sin, sizeof(struct sockaddr));
97 sin->sin_family = AF_INET;
98 if((sin->sin_addr.s_addr = inet_addr(addr)) == (in_addr_t)-1) {
99 hp = gethostbyname(addr);
101 syslog(LOG_ERR,"unknown host %s", addr);
104 sin->sin_family = hp->h_addrtype;
105 bcopy(hp->h_addr, (caddr_t)&sin->sin_addr, hp->h_length);
110 int tun_open(char *devname, struct sockaddr *ouraddr, char *theiraddr)
113 struct sockaddr_in *sin;
115 /* Open tun device */
116 tun = open (devname, O_RDWR);
118 syslog(LOG_ERR,"can't open %s - %m",devname);
123 * At first, name the interface.
125 bzero((char *)&ifra, sizeof(ifra));
126 bzero((char *)&ifrq, sizeof(ifrq));
128 strncpy(ifrq.ifr_name, devname+5, IFNAMSIZ);
129 strncpy(ifra.ifra_name, devname+5, IFNAMSIZ);
131 s = socket(AF_INET, SOCK_DGRAM, 0);
133 syslog(LOG_ERR,"can't open socket - %m");
138 * Delete (previous) addresses for interface
141 * On FreeBSD this ioctl returns error
142 * when tunN have no addresses, so - log and ignore it.
145 if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
146 syslog(LOG_ERR,"SIOCDIFADDR - %m");
150 * Set interface address
152 sin = (struct sockaddr_in *)&(ifra.ifra_addr);
153 bcopy(ouraddr, sin, sizeof(struct sockaddr_in));
154 sin->sin_len = sizeof(*sin);
157 * Set destination address
159 sin = (struct sockaddr_in *)&(ifra.ifra_broadaddr);
160 if(Set_address(theiraddr,sin)) {
161 syslog(LOG_ERR,"bad destination address: %s",theiraddr);
164 sin->sin_len = sizeof(*sin);
166 if (ioctl(s, SIOCAIFADDR, &ifra) < 0) {
167 syslog(LOG_ERR,"can't set interface address - %m");
172 * Now, bring up the interface.
174 if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
175 syslog(LOG_ERR,"can't get interface flags - %m");
179 ifrq.ifr_flags |= IFF_UP;
180 if (!(ioctl(s, SIOCSIFFLAGS, &ifrq) < 0)) {
184 syslog(LOG_ERR,"can't set interface UP - %m");
192 void Finish(int signum)
196 syslog(LOG_INFO,"exiting");
200 s = socket(AF_INET, SOCK_DGRAM, 0);
202 syslog(LOG_ERR,"can't open socket - %m");
207 * Shut down interface.
209 if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
210 syslog(LOG_ERR,"can't get interface flags - %m");
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");
221 * Delete addresses for interface
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");
237 int main (int argc, char **argv)
241 char *devname = NULL;
242 char *point_to = NULL;
243 char *to_point = NULL;
246 char *protocol = NULL;
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;
254 char buf[0x2000]; /* Packets buffer */
255 struct ip *ip = (struct ip *)buf;
257 fd_set rfds; /* File descriptors for select() */
258 int nfds; /* Return from select() */
259 int lastfd; /* highest fd we care about */
262 while ((c = getopt(argc, argv, "d:s:t:p:")) != -1) {
281 if ((argc != 1 && argc != 2) || (devname == NULL) ||
282 (point_to == NULL) || (to_point == NULL)) {
289 protnum = atoi(protocol);
294 source = *argv++; target = *argv;
297 /* Establish logging through 'syslog' */
298 openlog("nos-tun", LOG_PID, LOG_DAEMON);
300 if(Set_address(point_to, (struct sockaddr_in *)&t_laddr)) {
305 if(tun_open(devname, &t_laddr, to_point)) {
310 to = (struct sockaddr_in *)&whereto;
311 if(Set_address(target, to))
314 if ((net = socket(AF_INET, SOCK_RAW, protnum)) < 0) {
315 syslog(LOG_ERR,"can't open socket - %m");
320 if (Set_address(source, (struct sockaddr_in *)&wherefrom))
322 if (bind(net, &wherefrom, sizeof(wherefrom)) < 0) {
323 syslog(LOG_ERR, "can't bind source address - %m");
328 if (connect(net,&whereto,sizeof(struct sockaddr_in)) < 0 ) {
329 syslog(LOG_ERR,"can't connect to target - %m");
337 /* Install signal handlers */
338 (void)signal(SIGHUP,Finish);
339 (void)signal(SIGINT,Finish);
340 (void)signal(SIGTERM,Finish);
348 /* Set file descriptors for select() */
350 FD_SET(tun,&rfds); FD_SET(net,&rfds);
352 nfds = select(lastfd+1,&rfds,NULL,NULL,NULL);
354 syslog(LOG_ERR,"interrupted select");
358 if(nfds == 0) { /* Impossible ? */
359 syslog(LOG_ERR,"timeout in select");
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);
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");
392 "usage: nos-tun -t tunnel -s source -d destination -p protocol_number [source] target\n");