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 */
48 * Arguments: @int fd@ = file descriptor to read
49 * @unsigned mode@ = what's happened
50 * @void *v@ = pointer to tunnel block
54 * Use: Reads data from the tunnel.
57 static void t_read(int fd, unsigned mode, void *v)
63 n = read(fd, buf_i, sizeof(buf_i));
65 a_warn("TUN", "%s", p_ifname(t->p), "read-error", "?ERRNO", A_END);
68 IF_TRACING(T_TUNNEL, {
69 trace(T_TUNNEL, "tun-bsd: packet arrived");
70 trace_block(T_PACKET, "tun-bsd: packet contents", buf_i, n);
72 buf_init(&b, buf_i, n);
82 * Use: Initializes the tunneling system. Maybe this will require
83 * opening file descriptors or something.
86 static void t_init(void) { return; }
88 /* --- @t_create@ --- *
90 * Arguments: @peer *p@ = pointer to peer block
91 * @char **ifn@ = where to put the interface name
93 * Returns: A tunnel block if it worked, or null on failure.
95 * Use: Initializes a new tunnel.
98 static tunnel *t_create(peer *p, char **ifn)
107 sprintf(buf, "/dev/tun%u", n);
108 if ((fd = open(buf, O_RDWR)) >= 0)
112 T( trace(T_TUNNEL, "tunnel device %u busy: skipping", n); )
115 a_warn("TUN", "-", "bsd", "no-tunnel-devices", A_END);
118 a_warn("TUN", "-", "open-error", "%s", buf, "?ERRNO", A_END);
126 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
129 sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
131 *ifn = xstrdup(buf + 5);
132 T( trace(T_TUNNEL, "tun-bsd: attached interface %s to peer `%s'",
137 /* --- @t_inject@ --- *
139 * Arguments: @tunnel *t@ = pointer to tunnel block
140 * @buf *b@ = buffer to send
144 * Use: Injects a packet into the local network stack.
147 static void t_inject(tunnel *t, buf *b)
149 IF_TRACING(T_TUNNEL, {
150 trace(T_TUNNEL, "tun-bsd: inject decrypted packet");
151 trace_block(T_PACKET, "tun-bsd: packet contents", BBASE(b), BLEN(b));
153 write(t->f.fd, BBASE(b), BLEN(b));
156 /* --- @t_destroy@ --- *
158 * Arguments: @tunnel *t@ = pointer to tunnel block
162 * Use: Destroys a tunnel.
165 static void t_destroy(tunnel *t)
172 const tunnel_ops tun_bsd = {
183 /*----- That's all, folks -------------------------------------------------*/