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