| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: tun-unet.c,v 1.6 2004/04/08 01:36:17 mdw Exp $ |
| 4 | * |
| 5 | * Tunnel interface based on Linux Usernet |
| 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 | #include <sys/ioctl.h> |
| 34 | #include <net/if.h> |
| 35 | #include <unet.h> |
| 36 | |
| 37 | /*----- Main code ---------------------------------------------------------*/ |
| 38 | |
| 39 | #if TUN_TYPE != TUN_UNET |
| 40 | # error "Tunnel type mismatch: fix the Makefile" |
| 41 | #endif |
| 42 | |
| 43 | /* --- @t_read@ --- * |
| 44 | * |
| 45 | * Arguments: @int fd@ = file descriptor to read |
| 46 | * @unsigned mode@ = what's happened |
| 47 | * @void *v@ = pointer to tunnel block |
| 48 | * |
| 49 | * Returns: --- |
| 50 | * |
| 51 | * Use: Reads data from the tunnel. |
| 52 | */ |
| 53 | |
| 54 | static void t_read(int fd, unsigned mode, void *v) |
| 55 | { |
| 56 | tunnel *t = v; |
| 57 | ssize_t n; |
| 58 | buf b; |
| 59 | |
| 60 | n = read(fd, buf_i, sizeof(buf_i)); |
| 61 | if (n < 0) { |
| 62 | a_warn("tunnel read failed (%s): %s", tun_ifname(t), strerror(errno)); |
| 63 | return; |
| 64 | } |
| 65 | IF_TRACING(T_TUNNEL, { |
| 66 | trace(T_TUNNEL, "tunnel: packet arrived"); |
| 67 | trace_block(T_PACKET, "tunnel: packet contents", buf_i, n); |
| 68 | }) |
| 69 | buf_init(&b, buf_i, n); |
| 70 | p_tun(t->p, &b); |
| 71 | } |
| 72 | |
| 73 | /* --- @tun_init@ --- * |
| 74 | * |
| 75 | * Arguments: --- |
| 76 | * |
| 77 | * Returns: --- |
| 78 | * |
| 79 | * Use: Initializes the tunneling system. Maybe this will require |
| 80 | * opening file descriptors or something. |
| 81 | */ |
| 82 | |
| 83 | void tun_init(void) |
| 84 | { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | /* --- @tun_create@ --- * |
| 89 | * |
| 90 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 91 | * @peer *p@ = pointer to peer block |
| 92 | * |
| 93 | * Returns: Zero if it worked, nonzero on failure. |
| 94 | * |
| 95 | * Use: Initializes a new tunnel. |
| 96 | */ |
| 97 | |
| 98 | int tun_create(tunnel *t, peer *p) |
| 99 | { |
| 100 | int fd; |
| 101 | int f; |
| 102 | |
| 103 | if ((fd = open("/dev/unet", O_RDWR)) < 0) { |
| 104 | a_warn("open `/dev/unet' failed: %s", strerror(errno)); |
| 105 | return (-1); |
| 106 | } |
| 107 | fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
| 108 | if ((f = ioctl(fd, UNIOCGIFFLAGS)) < 0 || |
| 109 | ioctl(fd, UNIOCSIFFLAGS, f | IFF_POINTOPOINT)) { |
| 110 | a_warn("couldn't set point-to-point flag: %s", strerror(errno)); |
| 111 | close(fd); |
| 112 | return (-1); |
| 113 | } |
| 114 | t->p = p; |
| 115 | sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t); |
| 116 | sel_addfile(&t->f); |
| 117 | T( trace(T_TUNNEL, "tunnel: attached interface %s to peer `%s'", |
| 118 | tun_ifname(t), p_name(p)); ) |
| 119 | return (0); |
| 120 | } |
| 121 | |
| 122 | /* --- @tun_ifname@ --- * |
| 123 | * |
| 124 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 125 | * |
| 126 | * Returns: A pointer to the tunnel's interface name. |
| 127 | */ |
| 128 | |
| 129 | const char *tun_ifname(tunnel *t) |
| 130 | { |
| 131 | static char b[UNET_NAMEMAX]; |
| 132 | struct unet_info uni; |
| 133 | if (ioctl(t->f.fd, UNIOCGINFO, &uni)) { |
| 134 | a_warn("ioctl(UNIOCGINFO) failed: %s", strerror(errno)); |
| 135 | return ("<error>"); |
| 136 | } |
| 137 | if (strlen(uni.uni_ifname) + 1 > sizeof(b)) { |
| 138 | a_warn("interface name too long!"); |
| 139 | return ("<error>"); |
| 140 | } |
| 141 | strcpy(b, uni.uni_ifname); |
| 142 | return (b); |
| 143 | } |
| 144 | |
| 145 | /* --- @tun_inject@ --- * |
| 146 | * |
| 147 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 148 | * @buf *b@ = buffer to send |
| 149 | * |
| 150 | * Returns: --- |
| 151 | * |
| 152 | * Use: Injects a packet into the local network stack. |
| 153 | */ |
| 154 | |
| 155 | void tun_inject(tunnel *t, buf *b) |
| 156 | { |
| 157 | IF_TRACING(T_TUNNEL, { |
| 158 | trace(T_TUNNEL, "tunnel: inject decrypted packet"); |
| 159 | trace_block(T_PACKET, "tunnel: packet contents", BBASE(b), BLEN(b)); |
| 160 | }) |
| 161 | write(t->f.fd, BBASE(b), BLEN(b)); |
| 162 | } |
| 163 | |
| 164 | /* --- @tun_destroy@ --- * |
| 165 | * |
| 166 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 167 | * |
| 168 | * Returns: --- |
| 169 | * |
| 170 | * Use: Destroys a tunnel. |
| 171 | */ |
| 172 | |
| 173 | void tun_destroy(tunnel *t) |
| 174 | { |
| 175 | sel_rmfile(&t->f); |
| 176 | close(t->f.fd); |
| 177 | } |
| 178 | |
| 179 | /*----- That's all, folks -------------------------------------------------*/ |