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 /*----- Header files ------------------------------------------------------*/
35 /*----- Main code ---------------------------------------------------------*/
40 const tunnel_ops *ops; /* Pointer to operations */
41 sel_file f; /* Selector for tunnel device */
42 struct peer *p; /* Pointer to my peer */
43 unsigned n; /* Number of my tunnel device */
46 /* --- @t_ifname@ --- *
48 * Arguments: @tunnel *t@ = pointer to tunnel block
50 * Returns: A pointer to the tunnel's interface name.
53 static const char *t_ifname(tunnel *t)
56 sprintf(buf, "tun%u", t->n);
62 * Arguments: @int fd@ = file descriptor to read
63 * @unsigned mode@ = what's happened
64 * @void *v@ = pointer to tunnel block
68 * Use: Reads data from the tunnel.
71 static void t_read(int fd, unsigned mode, void *v)
77 n = read(fd, buf_i, sizeof(buf_i));
79 a_warn("TUN", "%s", t_ifname(t), "read-error", "?ERRNO", A_END);
82 IF_TRACING(T_TUNNEL, {
83 trace(T_TUNNEL, "tun-bsd: packet arrived");
84 trace_block(T_PACKET, "tun-bsd: packet contents", buf_i, n);
86 buf_init(&b, buf_i, n);
96 * Use: Initializes the tunneling system. Maybe this will require
97 * opening file descriptors or something.
100 static void t_init(void) { return; }
102 /* --- @t_create@ --- *
104 * Arguments: @peer *p@ = pointer to peer block
106 * Returns: A tunnel block if it worked, or null on failure.
108 * Use: Initializes a new tunnel.
111 static tunnel *t_create(peer *p)
120 sprintf(buf, "/dev/tun%u", n);
121 if ((fd = open(buf, O_RDWR)) >= 0)
125 T( trace(T_TUNNEL, "tunnel device %u busy: skipping", n); )
128 a_warn("TUN", "-", "bsd", "no-tunnel-devices", A_END);
131 a_warn("TUN", "-", "open-error", "%s", buf, "?ERRNO", A_END);
139 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
142 sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
144 T( trace(T_TUNNEL, "tun-bsd: attached interface %s to peer `%s'",
145 t_ifname(t), p_name(p)); )
149 /* --- @t_inject@ --- *
151 * Arguments: @tunnel *t@ = pointer to tunnel block
152 * @buf *b@ = buffer to send
156 * Use: Injects a packet into the local network stack.
159 static void t_inject(tunnel *t, buf *b)
161 IF_TRACING(T_TUNNEL, {
162 trace(T_TUNNEL, "tun-bsd: inject decrypted packet");
163 trace_block(T_PACKET, "tun-bsd: packet contents", BBASE(b), BLEN(b));
165 write(t->f.fd, BBASE(b), BLEN(b));
168 /* --- @t_destroy@ --- *
170 * Arguments: @tunnel *t@ = pointer to tunnel block
174 * Use: Destroys a tunnel.
177 static void t_destroy(tunnel *t)
184 const tunnel_ops tun_bsd = {
195 /*----- That's all, folks -------------------------------------------------*/