3 * $Id: tun-bsd.c,v 1.1 2001/02/05 19:48:18 mdw Exp $
5 * Tunnel interface for 4.4BSD-derived systems
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 2001/02/05 19:48:18 mdw
33 * Initial support for BSD tunnel devices.
37 /*----- Header files ------------------------------------------------------*/
41 /*----- Main code ---------------------------------------------------------*/
43 #if TUN_TYPE != TUN_BSD
44 # error "Tunnel type mismatch: fix the Makefile"
49 * Arguments: @int fd@ = file descriptor to read
50 * @unsigned mode@ = what's happened
51 * @void *v@ = pointer to tunnel block
55 * Use: Reads data from the tunnel.
58 void t_read(int fd, unsigned mode, void *v)
64 n = read(fd, buf_i, sizeof(buf_i));
66 a_warn("tunnel read failed (%s): %s", tun_ifname(t), strerror(errno));
69 IF_TRACING(T_TUNNEL, {
70 trace(T_TUNNEL, "tunnel: packet arrived");
71 trace_block(T_PACKET, "tunnel: packet contents", buf_i, n);
73 buf_init(&b, buf_i, n);
77 /* --- @tun_init@ --- *
83 * Use: Initializes the tunneling system. Maybe this will require
84 * opening file descriptors or something.
92 /* --- @tun_create@ --- *
94 * Arguments: @tunnel *t@ = pointer to tunnel block
95 * @peer *p@ = pointer to peer block
97 * Returns: Zero if it worked, nonzero on failure.
99 * Use: Initializes a new tunnel.
102 int tun_create(tunnel *t, peer *p)
110 sprintf(buf, "/dev/tun%u", n);
111 if ((fd = open("/dev/unet", O_RDWR)) >= 0)
115 T( trace(T_TUNNEL, "tunnel device %u busy: skipping", n); )
118 a_warn("no suitable tunnel devices found");
121 a_warn("error opening `%s': %s (skipping)", buf, strerror(errno));
127 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
130 sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
132 T( trace(T_TUNNEL, "tunnel: attached interface %s to peer `%s'",
133 tun_ifname(t), p_name(p)); )
137 /* --- @tun_ifname@ --- *
139 * Arguments: @tunnel *t@ = pointer to tunnel block
141 * Returns: A pointer to the tunnel's interface name.
144 const char *tun_ifname(tunnel *t)
147 sprintf(buf, "tun%u", t->n);
151 /* --- @tun_inject@ --- *
153 * Arguments: @tunnel *t@ = pointer to tunnel block
154 * @buf *b@ = buffer to send
158 * Use: Injects a packet into the local network stack.
161 void tun_inject(tunnel *t, buf *b)
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));
167 write(t->f.fd, BBASE(b), BLEN(b));
170 /* --- @tun_destroy@ --- *
172 * Arguments: @tunnel *t@ = pointer to tunnel block
176 * Use: Destroys a tunnel.
179 void tun_destroy(tunnel *t)
185 /*----- That's all, folks -------------------------------------------------*/