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