chiark / gitweb /
Merge branch 'public'
[tripe] / server / tun-bsd.c
CommitLineData
fd528bde 1/* -*-c-*-
fd528bde 2 *
3 * Tunnel interface for 4.4BSD-derived systems
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
fd528bde 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
e04c2d50 16 *
fd528bde 17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
e04c2d50 21 *
fd528bde 22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
fd528bde 27/*----- Header files ------------------------------------------------------*/
28
42da2a58 29#define TUN_INTERNALS
30
fd528bde 31#include "tripe.h"
32
33/*----- Main code ---------------------------------------------------------*/
34
42da2a58 35#ifdef TUN_BSD
36
37struct tunnel {
38 const tunnel_ops *ops; /* Pointer to operations */
39 sel_file f; /* Selector for tunnel device */
40 struct peer *p; /* Pointer to my peer */
41 unsigned n; /* Number of my tunnel device */
e04c2d50 42};
42da2a58 43
fd528bde 44/* --- @t_read@ --- *
45 *
46 * Arguments: @int fd@ = file descriptor to read
47 * @unsigned mode@ = what's happened
48 * @void *v@ = pointer to tunnel block
49 *
50 * Returns: ---
51 *
52 * Use: Reads data from the tunnel.
53 */
54
42da2a58 55static void t_read(int fd, unsigned mode, void *v)
fd528bde 56{
57 tunnel *t = v;
58 ssize_t n;
59 buf b;
60
61 n = read(fd, buf_i, sizeof(buf_i));
62 if (n < 0) {
72917fe7 63 a_warn("TUN", "%s", p_ifname(t->p), "read-error", "?ERRNO", A_END);
fd528bde 64 return;
65 }
66 IF_TRACING(T_TUNNEL, {
060ca767 67 trace(T_TUNNEL, "tun-bsd: packet arrived");
68 trace_block(T_PACKET, "tun-bsd: packet contents", buf_i, n);
fd528bde 69 })
70 buf_init(&b, buf_i, n);
71 p_tun(t->p, &b);
72}
73
42da2a58 74/* --- @t_init@ --- *
fd528bde 75 *
76 * Arguments: ---
77 *
78 * Returns: ---
79 *
80 * Use: Initializes the tunneling system. Maybe this will require
81 * opening file descriptors or something.
82 */
83
42da2a58 84static void t_init(void) { return; }
fd528bde 85
42da2a58 86/* --- @t_create@ --- *
fd528bde 87 *
42da2a58 88 * Arguments: @peer *p@ = pointer to peer block
72917fe7 89 * @char **ifn@ = where to put the interface name
fd528bde 90 *
42da2a58 91 * Returns: A tunnel block if it worked, or null on failure.
fd528bde 92 *
93 * Use: Initializes a new tunnel.
94 */
95
72917fe7 96static tunnel *t_create(peer *p, char **ifn)
fd528bde 97{
98 int fd;
99 unsigned n;
42da2a58 100 tunnel *t;
fd528bde 101 char buf[16];
102
103 n = 0;
104 for (;;) {
105 sprintf(buf, "/dev/tun%u", n);
ef4a1ab7 106 if ((fd = open(buf, O_RDWR)) >= 0)
fd528bde 107 break;
108 switch (errno) {
109 case EBUSY:
110 T( trace(T_TUNNEL, "tunnel device %u busy: skipping", n); )
111 break;
112 case ENOENT:
f43df819 113 a_warn("TUN", "-", "bsd", "no-tunnel-devices", A_END);
42da2a58 114 return (0);
fd528bde 115 default:
f43df819 116 a_warn("TUN", "-", "open-error", "%s", buf, "?ERRNO", A_END);
fd528bde 117 break;
118 }
119 n++;
120 }
121
42da2a58 122 t = CREATE(tunnel);
123 t->ops = &tun_bsd;
fd528bde 124 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
125 t->p = p;
126 t->n = n;
127 sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
128 sel_addfile(&t->f);
72917fe7 129 *ifn = xstrdup(buf + 5);
060ca767 130 T( trace(T_TUNNEL, "tun-bsd: attached interface %s to peer `%s'",
72917fe7 131 *ifn, p_name(p)); )
42da2a58 132 return (t);
fd528bde 133}
134
42da2a58 135/* --- @t_inject@ --- *
fd528bde 136 *
137 * Arguments: @tunnel *t@ = pointer to tunnel block
138 * @buf *b@ = buffer to send
139 *
140 * Returns: ---
141 *
142 * Use: Injects a packet into the local network stack.
143 */
144
42da2a58 145static void t_inject(tunnel *t, buf *b)
fd528bde 146{
147 IF_TRACING(T_TUNNEL, {
060ca767 148 trace(T_TUNNEL, "tun-bsd: inject decrypted packet");
149 trace_block(T_PACKET, "tun-bsd: packet contents", BBASE(b), BLEN(b));
fd528bde 150 })
151 write(t->f.fd, BBASE(b), BLEN(b));
152}
153
42da2a58 154/* --- @t_destroy@ --- *
fd528bde 155 *
156 * Arguments: @tunnel *t@ = pointer to tunnel block
157 *
158 * Returns: ---
159 *
160 * Use: Destroys a tunnel.
161 */
162
42da2a58 163static void t_destroy(tunnel *t)
6047fbac 164 { sel_rmfile(&t->f); close(t->f.fd); DESTROY(t); }
fd528bde 165
42da2a58 166const tunnel_ops tun_bsd = {
167 "bsd",
168 t_init,
169 t_create,
72917fe7 170 0,
42da2a58 171 t_inject,
172 t_destroy
173};
174
175#endif
176
fd528bde 177/*----- That's all, folks -------------------------------------------------*/