1 /* $Gateweaver: tunkq.c,v 1.2 2003/11/27 22:47:41 cmaxwell Exp $ */ 2 /* 3 * Copyright 2003 Christopher J. Maxwell <cmaxwell@themanor.net> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following 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 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 #include <sys/types.h> 29 #include <sys/socket.h> 30 #include <sys/uio.h> 31 #include <netinet/in.h> 32 #include <net/if.h> 33 #include <net/if_tun.h> 34 #include <errno.h> 35 #include <err.h> 36 #include <event.h> 37 #include <fcntl.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <unistd.h> 41 42 #define TUN0_ADDR "172.16.0.1" 43 #define TUN1_ADDR "172.16.0.2" 44 #define TUN_MAXWAIT 5 45 #define TUN_PINGDEL 1 46 47 struct buffer { 48 u_char *buf; 49 size_t len; 50 size_t a; 51 }; 52 53 int state; 54 int tunfd[2]; 55 struct buffer tpkt; 56 u_char pktbuf[TUNMTU]; 57 struct event tunwev[2]; 58 struct timeval exittv = {TUN_MAXWAIT, 0}; 59 60 void 61 tunnel_write(int fd, short which, void *arg) 62 { 63 uint32_t type = htonl(AF_INET); 64 struct iovec iv[2]; 65 int rlen; 66 int fdkey = (fd == tunfd[0]) ? 0 : 1; 67 68 iv[0].iov_base = &type; 69 iv[0].iov_len = sizeof(type); 70 iv[1].iov_base = tpkt.buf; 71 iv[1].iov_len = tpkt.len; 72 73 state++; 74 if ((rlen = writev(fd, iv, 2)) > 0) 75 fprintf(stderr, "Tunnel %d wrote %d bytes\n", 76 fdkey, rlen - sizeof(type)); 77 else 78 errx(1, "Read from tunnel %d failed", fdkey); 79 } 80 81 void 82 tunnel_read(int fd, short which, void *arg) 83 { 84 struct iovec iv[2]; 85 uint32_t type; 86 int rlen; 87 int fdkey = (fd == tunfd[0]) ? 0 : 1; 88 int oppfdkey = (fd == tunfd[0]) ? 1 : 0; 89 90 iv[0].iov_base = &type; 91 iv[0].iov_len = sizeof(type); 92 iv[1].iov_base = tpkt.buf; 93 iv[1].iov_len = tpkt.a; 94 95 state++; 96 if ((rlen = readv(fd, iv, 2)) > 0) { 97 fprintf(stderr, "Tunnel %d read %d bytes\n", 98 fdkey, rlen - sizeof(type)); 99 tpkt.len = rlen - sizeof(type); 100 101 /* add write event on opposite tunnel */ 102 event_add(&tunwev[oppfdkey], &exittv); 103 } else 104 errx(1, "Read from tunnel %d failed", fdkey); 105 } 106 107 void 108 tunnel_ping(int fd, short which, void *arg) 109 { 110 system("ping -c 1 -I " TUN0_ADDR " " TUN1_ADDR " >/dev/null &"); 111 } 112 113 /* 114 * +------------+ +------------+ 115 * | tun0 | | tun1 | 116 * | 172.16.0.1 | | 172.16.0.2 | 117 * +------------+ +------------+ 118 * 119 * Set up both tunnel devices (tun0, tun1) 120 * This works because the routing table prefers the opposing end of the ptp 121 * interfaces. 122 * Set up one read and one write event per tunnel. 123 * The read events add the write event. 124 */ 125 int 126 do_tun(void) 127 { 128 struct event tunrev[2]; 129 struct event pingev; 130 struct timeval pingtv = {TUN_PINGDEL, 0}; 131 132 /* read buffer */ 133 tpkt.buf = (u_char *)&pktbuf; 134 tpkt.len = 0; 135 tpkt.a = sizeof(pktbuf); 136 137 event_init(); 138 139 /* tun0 */ 140 if ((tunfd[0] = open("/dev/tun0", O_RDWR)) < 0) 141 errx(1, "Cannot open /dev/tun0"); 142 event_set(&tunrev[0], tunfd[0], EV_READ, tunnel_read, NULL); 143 event_set(&tunwev[0], tunfd[0], EV_WRITE, tunnel_write, NULL); 144 event_add(&tunrev[0], &exittv); 145 146 /* tun1 */ 147 if ((tunfd[1] = open("/dev/tun1", O_RDWR)) < 0) 148 errx(1, "Cannot open /dev/tun1"); 149 event_set(&tunrev[1], tunfd[1], EV_READ, tunnel_read, NULL); 150 event_set(&tunwev[1], tunfd[1], EV_WRITE, tunnel_write, NULL); 151 event_add(&tunrev[1], &exittv); 152 153 /* ping */ 154 evtimer_set(&pingev, tunnel_ping, NULL); 155 event_add(&pingev, &pingtv); 156 157 /* configure the interfaces */ 158 system("ifconfig tun0 " TUN0_ADDR 159 " netmask 255.255.255.255 " TUN1_ADDR); 160 system("ifconfig tun1 " TUN1_ADDR 161 " netmask 255.255.255.255 " TUN0_ADDR); 162 163 state = 0; 164 if (event_dispatch() < 0) 165 errx(errno, "Event handler failed"); 166 167 return (state != 4); 168 } 169