1*bd35765dSbluhm /* $OpenBSD: kqueue-tun.c,v 1.5 2016/09/20 23:05:27 bluhm Exp $ */
29127188dSmickey /* $Gateweaver: tunkq.c,v 1.2 2003/11/27 22:47:41 cmaxwell Exp $ */
39127188dSmickey /*
49127188dSmickey * Copyright 2003 Christopher J. Maxwell <cmaxwell@themanor.net>
59127188dSmickey * All rights reserved.
69127188dSmickey *
79127188dSmickey * Redistribution and use in source and binary forms, with or without
89127188dSmickey * modification, are permitted provided that the following conditions
99127188dSmickey * are met:
109127188dSmickey * 1. Redistributions of source code must retain the above copyright
119127188dSmickey * notice, this list of conditions and the following disclaimer.
129127188dSmickey * 2. Redistributions in binary form must reproduce the above copyright
139127188dSmickey * notice, this list of conditions and the following disclaimer in the
149127188dSmickey * documentation and/or other materials provided with the distribution.
159127188dSmickey * 3. The name of the author may not be used to endorse or promote products
169127188dSmickey * derived from this software without specific prior written permission.
179127188dSmickey *
189127188dSmickey * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
199127188dSmickey * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
209127188dSmickey * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
219127188dSmickey * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
229127188dSmickey * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
239127188dSmickey * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
249127188dSmickey * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
259127188dSmickey * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
269127188dSmickey * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
279127188dSmickey * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
289127188dSmickey */
29*bd35765dSbluhm
309127188dSmickey #include <sys/types.h>
319127188dSmickey #include <sys/socket.h>
329127188dSmickey #include <sys/uio.h>
33*bd35765dSbluhm
349127188dSmickey #include <netinet/in.h>
359127188dSmickey #include <net/if.h>
369127188dSmickey #include <net/if_tun.h>
37*bd35765dSbluhm
389127188dSmickey #include <err.h>
39*bd35765dSbluhm #include <errno.h>
409127188dSmickey #include <event.h>
419127188dSmickey #include <fcntl.h>
429127188dSmickey #include <stdio.h>
439127188dSmickey #include <stdlib.h>
449127188dSmickey #include <unistd.h>
459127188dSmickey
46*bd35765dSbluhm #include "main.h"
47*bd35765dSbluhm
48a8831fb0Smarkus #define TUN0 "tun98"
49a8831fb0Smarkus #define TUN1 "tun99"
50a8831fb0Smarkus #define TUN0_ADDR "192.0.2.1"
51a8831fb0Smarkus #define TUN1_ADDR "192.0.2.2"
529127188dSmickey #define TUN_MAXWAIT 5
539127188dSmickey #define TUN_PINGDEL 1
549127188dSmickey
559127188dSmickey struct buffer {
569127188dSmickey u_char *buf;
579127188dSmickey size_t len;
589127188dSmickey size_t a;
599127188dSmickey };
609127188dSmickey
619127188dSmickey int state;
629127188dSmickey int tunfd[2];
639127188dSmickey struct buffer tpkt;
649127188dSmickey u_char pktbuf[TUNMTU];
659127188dSmickey struct event tunwev[2];
669127188dSmickey struct timeval exittv = {TUN_MAXWAIT, 0};
679127188dSmickey
689127188dSmickey void
tunnel_write(int fd,short which,void * arg)699127188dSmickey tunnel_write(int fd, short which, void *arg)
709127188dSmickey {
719127188dSmickey uint32_t type = htonl(AF_INET);
729127188dSmickey struct iovec iv[2];
739127188dSmickey int rlen;
749127188dSmickey int fdkey = (fd == tunfd[0]) ? 0 : 1;
759127188dSmickey
769127188dSmickey iv[0].iov_base = &type;
779127188dSmickey iv[0].iov_len = sizeof(type);
789127188dSmickey iv[1].iov_base = tpkt.buf;
799127188dSmickey iv[1].iov_len = tpkt.len;
809127188dSmickey
819127188dSmickey state++;
829127188dSmickey if ((rlen = writev(fd, iv, 2)) > 0)
8319fd5564Sderaadt fprintf(stderr, "Tunnel %d wrote %ld bytes\n",
8419fd5564Sderaadt fdkey, (long)(rlen - sizeof(type)));
859127188dSmickey else
86fda43b0cSblambert errx(1, "Write to tunnel %d failed", fdkey);
879127188dSmickey }
889127188dSmickey
899127188dSmickey void
tunnel_read(int fd,short which,void * arg)909127188dSmickey tunnel_read(int fd, short which, void *arg)
919127188dSmickey {
929127188dSmickey struct iovec iv[2];
939127188dSmickey uint32_t type;
949127188dSmickey int rlen;
959127188dSmickey int fdkey = (fd == tunfd[0]) ? 0 : 1;
969127188dSmickey int oppfdkey = (fd == tunfd[0]) ? 1 : 0;
979127188dSmickey
989127188dSmickey iv[0].iov_base = &type;
999127188dSmickey iv[0].iov_len = sizeof(type);
1009127188dSmickey iv[1].iov_base = tpkt.buf;
1019127188dSmickey iv[1].iov_len = tpkt.a;
1029127188dSmickey
1039127188dSmickey state++;
1049127188dSmickey if ((rlen = readv(fd, iv, 2)) > 0) {
10519fd5564Sderaadt fprintf(stderr, "Tunnel %d read %ld bytes\n",
10619fd5564Sderaadt fdkey, (long)(rlen - sizeof(type)));
1079127188dSmickey tpkt.len = rlen - sizeof(type);
1089127188dSmickey
1099127188dSmickey /* add write event on opposite tunnel */
1109127188dSmickey event_add(&tunwev[oppfdkey], &exittv);
1119127188dSmickey } else
1129127188dSmickey errx(1, "Read from tunnel %d failed", fdkey);
1139127188dSmickey }
1149127188dSmickey
1159127188dSmickey void
tunnel_ping(int fd,short which,void * arg)1169127188dSmickey tunnel_ping(int fd, short which, void *arg)
1179127188dSmickey {
1189127188dSmickey system("ping -c 1 -I " TUN0_ADDR " " TUN1_ADDR " >/dev/null &");
1199127188dSmickey }
1209127188dSmickey
1219127188dSmickey /*
1229127188dSmickey * +------------+ +------------+
123a8831fb0Smarkus * | TUN0 | | TUN1 |
124a8831fb0Smarkus * | TUN0_ADDR | | TUN1_ADDR |
1259127188dSmickey * +------------+ +------------+
1269127188dSmickey *
127a8831fb0Smarkus * Set up both tunnel devices (TUN0, TUN1)
1289127188dSmickey * This works because the routing table prefers the opposing end of the ptp
1299127188dSmickey * interfaces.
1309127188dSmickey * Set up one read and one write event per tunnel.
1319127188dSmickey * The read events add the write event.
1329127188dSmickey */
1339127188dSmickey int
do_tun(void)1349127188dSmickey do_tun(void)
1359127188dSmickey {
1369127188dSmickey struct event tunrev[2];
1379127188dSmickey struct event pingev;
1389127188dSmickey struct timeval pingtv = {TUN_PINGDEL, 0};
1399127188dSmickey
1409127188dSmickey /* read buffer */
1419127188dSmickey tpkt.buf = (u_char *)&pktbuf;
1429127188dSmickey tpkt.len = 0;
1439127188dSmickey tpkt.a = sizeof(pktbuf);
1449127188dSmickey
1459127188dSmickey event_init();
1469127188dSmickey
1479127188dSmickey /* tun0 */
148a8831fb0Smarkus if ((tunfd[0] = open("/dev/" TUN0, O_RDWR)) < 0)
149a8831fb0Smarkus errx(1, "Cannot open /dev/" TUN0);
1509127188dSmickey event_set(&tunrev[0], tunfd[0], EV_READ, tunnel_read, NULL);
1519127188dSmickey event_set(&tunwev[0], tunfd[0], EV_WRITE, tunnel_write, NULL);
1529127188dSmickey event_add(&tunrev[0], &exittv);
1539127188dSmickey
1549127188dSmickey /* tun1 */
155a8831fb0Smarkus if ((tunfd[1] = open("/dev/" TUN1, O_RDWR)) < 0)
156a8831fb0Smarkus errx(1, "Cannot open /dev/" TUN1);
1579127188dSmickey event_set(&tunrev[1], tunfd[1], EV_READ, tunnel_read, NULL);
1589127188dSmickey event_set(&tunwev[1], tunfd[1], EV_WRITE, tunnel_write, NULL);
1599127188dSmickey event_add(&tunrev[1], &exittv);
1609127188dSmickey
1619127188dSmickey /* ping */
1629127188dSmickey evtimer_set(&pingev, tunnel_ping, NULL);
1639127188dSmickey event_add(&pingev, &pingtv);
1649127188dSmickey
1659127188dSmickey /* configure the interfaces */
166a8831fb0Smarkus system("ifconfig " TUN0 " " TUN0_ADDR
1679127188dSmickey " netmask 255.255.255.255 " TUN1_ADDR);
168a8831fb0Smarkus system("ifconfig " TUN1 " " TUN1_ADDR
1699127188dSmickey " netmask 255.255.255.255 " TUN0_ADDR);
1709127188dSmickey
1719127188dSmickey state = 0;
1729127188dSmickey if (event_dispatch() < 0)
1739127188dSmickey errx(errno, "Event handler failed");
1749127188dSmickey
1759127188dSmickey return (state != 4);
1769127188dSmickey }
177