| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id$ |
| 4 | * |
| 5 | * Tunnel interface for 4.4BSD-derived systems |
| 6 | * |
| 7 | * (c) 2001 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Trivial IP Encryption (TrIPE). |
| 13 | * |
| 14 | * TrIPE is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * TrIPE is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with TrIPE; if not, write to the Free Software Foundation, |
| 26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | */ |
| 28 | |
| 29 | /*----- Header files ------------------------------------------------------*/ |
| 30 | |
| 31 | #include "tripe.h" |
| 32 | |
| 33 | /*----- Main code ---------------------------------------------------------*/ |
| 34 | |
| 35 | #if TUN_TYPE != TUN_BSD |
| 36 | # error "Tunnel type mismatch: fix the Makefile" |
| 37 | #endif |
| 38 | |
| 39 | /* --- @t_read@ --- * |
| 40 | * |
| 41 | * Arguments: @int fd@ = file descriptor to read |
| 42 | * @unsigned mode@ = what's happened |
| 43 | * @void *v@ = pointer to tunnel block |
| 44 | * |
| 45 | * Returns: --- |
| 46 | * |
| 47 | * Use: Reads data from the tunnel. |
| 48 | */ |
| 49 | |
| 50 | void t_read(int fd, unsigned mode, void *v) |
| 51 | { |
| 52 | tunnel *t = v; |
| 53 | ssize_t n; |
| 54 | buf b; |
| 55 | |
| 56 | n = read(fd, buf_i, sizeof(buf_i)); |
| 57 | if (n < 0) { |
| 58 | a_warn("TUN %s read-error -- %s", tun_ifname(t), strerror(errno)); |
| 59 | return; |
| 60 | } |
| 61 | IF_TRACING(T_TUNNEL, { |
| 62 | trace(T_TUNNEL, "tunnel: packet arrived"); |
| 63 | trace_block(T_PACKET, "tunnel: packet contents", buf_i, n); |
| 64 | }) |
| 65 | buf_init(&b, buf_i, n); |
| 66 | p_tun(t->p, &b); |
| 67 | } |
| 68 | |
| 69 | /* --- @tun_init@ --- * |
| 70 | * |
| 71 | * Arguments: --- |
| 72 | * |
| 73 | * Returns: --- |
| 74 | * |
| 75 | * Use: Initializes the tunneling system. Maybe this will require |
| 76 | * opening file descriptors or something. |
| 77 | */ |
| 78 | |
| 79 | void tun_init(void) |
| 80 | { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | /* --- @tun_create@ --- * |
| 85 | * |
| 86 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 87 | * @peer *p@ = pointer to peer block |
| 88 | * |
| 89 | * Returns: Zero if it worked, nonzero on failure. |
| 90 | * |
| 91 | * Use: Initializes a new tunnel. |
| 92 | */ |
| 93 | |
| 94 | int tun_create(tunnel *t, peer *p) |
| 95 | { |
| 96 | int fd; |
| 97 | unsigned n; |
| 98 | char buf[16]; |
| 99 | |
| 100 | n = 0; |
| 101 | for (;;) { |
| 102 | sprintf(buf, "/dev/tun%u", n); |
| 103 | if ((fd = open(buf, O_RDWR)) >= 0) |
| 104 | break; |
| 105 | switch (errno) { |
| 106 | case EBUSY: |
| 107 | T( trace(T_TUNNEL, "tunnel device %u busy: skipping", n); ) |
| 108 | break; |
| 109 | case ENOENT: |
| 110 | a_warn("TUN - bsd no-tunnel-devices"); |
| 111 | return (-1); |
| 112 | default: |
| 113 | a_warn("TUN - open-error %s -- %s", buf, strerror(errno)); |
| 114 | break; |
| 115 | } |
| 116 | n++; |
| 117 | } |
| 118 | |
| 119 | fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
| 120 | t->p = p; |
| 121 | t->n = n; |
| 122 | sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t); |
| 123 | sel_addfile(&t->f); |
| 124 | T( trace(T_TUNNEL, "tunnel: attached interface %s to peer `%s'", |
| 125 | tun_ifname(t), p_name(p)); ) |
| 126 | return (0); |
| 127 | } |
| 128 | |
| 129 | /* --- @tun_ifname@ --- * |
| 130 | * |
| 131 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 132 | * |
| 133 | * Returns: A pointer to the tunnel's interface name. |
| 134 | */ |
| 135 | |
| 136 | const char *tun_ifname(tunnel *t) |
| 137 | { |
| 138 | static char buf[8]; |
| 139 | sprintf(buf, "tun%u", t->n); |
| 140 | return (buf); |
| 141 | } |
| 142 | |
| 143 | /* --- @tun_inject@ --- * |
| 144 | * |
| 145 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 146 | * @buf *b@ = buffer to send |
| 147 | * |
| 148 | * Returns: --- |
| 149 | * |
| 150 | * Use: Injects a packet into the local network stack. |
| 151 | */ |
| 152 | |
| 153 | void tun_inject(tunnel *t, buf *b) |
| 154 | { |
| 155 | IF_TRACING(T_TUNNEL, { |
| 156 | trace(T_TUNNEL, "tunnel: inject decrypted packet"); |
| 157 | trace_block(T_PACKET, "tunnel: packet contents", BBASE(b), BLEN(b)); |
| 158 | }) |
| 159 | write(t->f.fd, BBASE(b), BLEN(b)); |
| 160 | } |
| 161 | |
| 162 | /* --- @tun_destroy@ --- * |
| 163 | * |
| 164 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 165 | * |
| 166 | * Returns: --- |
| 167 | * |
| 168 | * Use: Destroys a tunnel. |
| 169 | */ |
| 170 | |
| 171 | void tun_destroy(tunnel *t) |
| 172 | { |
| 173 | sel_rmfile(&t->f); |
| 174 | close(t->f.fd); |
| 175 | } |
| 176 | |
| 177 | /*----- That's all, folks -------------------------------------------------*/ |