chiark / gitweb /
peer, tunnels: New file-descriptor opening interface.
[tripe] / server / tun-unet.c
CommitLineData
410c8acf 1/* -*-c-*-
410c8acf 2 *
3 * Tunnel interface based on Linux Usernet
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
410c8acf 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 *
410c8acf 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 *
410c8acf 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
410c8acf 27/*----- Header files ------------------------------------------------------*/
28
42da2a58 29#define TUN_INTERNALS
30
410c8acf 31#include "tripe.h"
32
42da2a58 33#ifdef TUN_UNET
34# include <sys/ioctl.h>
35# include <net/if.h>
36# include <unet.h>
37#endif
410c8acf 38
39/*----- Main code ---------------------------------------------------------*/
40
42da2a58 41#ifdef TUN_UNET
42
43struct tunnel {
44 const tunnel_ops *ops; /* Pointer to operations */
45 sel_file f; /* Selector for Usernet device */
46 struct peer *p; /* Pointer to my peer */
47};
48
410c8acf 49/* --- @t_read@ --- *
50 *
51 * Arguments: @int fd@ = file descriptor to read
52 * @unsigned mode@ = what's happened
53 * @void *v@ = pointer to tunnel block
54 *
55 * Returns: ---
56 *
57 * Use: Reads data from the tunnel.
58 */
59
60a837d8 60static void t_read(int fd, unsigned mode, void *v)
410c8acf 61{
62 tunnel *t = v;
63 ssize_t n;
64 buf b;
65
66 n = read(fd, buf_i, sizeof(buf_i));
67 if (n < 0) {
72917fe7 68 a_warn("TUN", "%s", p_ifname(t->p), "read-error", "?ERRNO", A_END);
410c8acf 69 return;
70 }
71 IF_TRACING(T_TUNNEL, {
060ca767 72 trace(T_TUNNEL, "tun-unet: packet arrived");
73 trace_block(T_PACKET, "tun-unet: packet contents", buf_i, n);
410c8acf 74 })
75 buf_init(&b, buf_i, n);
76 p_tun(t->p, &b);
77}
78
42da2a58 79/* --- @t_init@ --- *
410c8acf 80 *
81 * Arguments: ---
82 *
83 * Returns: ---
84 *
85 * Use: Initializes the tunneling system. Maybe this will require
86 * opening file descriptors or something.
87 */
88
42da2a58 89static void t_init(void) { return; }
410c8acf 90
eb5f3fea 91/* --- @t_open@ --- *
410c8acf 92 *
eb5f3fea 93 * Arguments: @char **ifn@ = where to put the interface name
410c8acf 94 *
eb5f3fea 95 * Returns: A file descriptor, or @-1@ on failure.
410c8acf 96 *
eb5f3fea
MW
97 * Use: Opens a tunnel device. This will run with root privileges
98 * even if the rest of the server has dropped them.
410c8acf 99 */
100
eb5f3fea 101static int t_open(char **ifn)
410c8acf 102{
103 int fd;
a368bfbc 104 int f;
72917fe7 105 struct unet_info uni;
410c8acf 106
107 if ((fd = open("/dev/unet", O_RDWR)) < 0) {
f43df819 108 a_warn("TUN", "-", "unet", "open-error", "/dev/unet", "?ERRNO", A_END);
eb5f3fea 109 goto fail_0;
410c8acf 110 }
a368bfbc 111 if ((f = ioctl(fd, UNIOCGIFFLAGS)) < 0 ||
112 ioctl(fd, UNIOCSIFFLAGS, f | IFF_POINTOPOINT)) {
f43df819 113 a_warn("TUN", "-", "unet", "config-error", "?ERRNO", A_END);
eb5f3fea 114 goto fail_1;
a368bfbc 115 }
eb5f3fea
MW
116 if (ioctl(t->f.fd, UNIOCGINFO, &uni)) {
117 a_warn("TUN", "-", "unet", "getinfo-error", "?ERRNO", A_END);
118 goto fail_1;
119 }
120 *ifn = xstrdup(uni.uni_ifname);
121 return (fd);
122
123fail_1:
124 close(fd);
125fail_0:
126 return (-1);
127}
128
129/* --- @t_create@ --- *
130 *
131 * Arguments: @peer *p@ = pointer to peer block
132 * @int fd@ = file descriptor of tunnel device
133 * @char **ifn@ = where to put the interface name
134 *
135 * Returns: A tunnel block if it worked, or null on failure.
136 *
137 * Use: Initializes a new tunnel.
138 */
139
140static tunnel *t_create(peer *p, int fd, char **ifn)
141{
142 tunnel *t;
143
144 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
42da2a58 145 t = CREATE(tunnel);
146 t->ops = &tun_unet;
410c8acf 147 t->p = p;
148 sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
149 sel_addfile(&t->f);
42da2a58 150 return (t);
410c8acf 151}
152
42da2a58 153/* --- @t_inject@ --- *
410c8acf 154 *
155 * Arguments: @tunnel *t@ = pointer to tunnel block
156 * @buf *b@ = buffer to send
157 *
158 * Returns: ---
159 *
160 * Use: Injects a packet into the local network stack.
161 */
162
42da2a58 163static void t_inject(tunnel *t, buf *b)
410c8acf 164{
165 IF_TRACING(T_TUNNEL, {
060ca767 166 trace(T_TUNNEL, "tun-unet: inject decrypted packet");
167 trace_block(T_PACKET, "tun-unet: packet contents", BBASE(b), BLEN(b));
410c8acf 168 })
169 write(t->f.fd, BBASE(b), BLEN(b));
170}
171
42da2a58 172/* --- @t_destroy@ --- *
410c8acf 173 *
174 * Arguments: @tunnel *t@ = pointer to tunnel block
175 *
176 * Returns: ---
177 *
178 * Use: Destroys a tunnel.
179 */
180
42da2a58 181static void t_destroy(tunnel *t)
6047fbac 182 { sel_rmfile(&t->f); close(t->f.fd); DESTROY(t); }
410c8acf 183
42da2a58 184const tunnel_ops tun_unet = {
185 "unet",
186 t_init,
eb5f3fea 187 t_open,
42da2a58 188 t_create,
72917fe7 189 0,
42da2a58 190 t_inject,
191 t_destroy
192};
193
194#endif
195
410c8acf 196/*----- That's all, folks -------------------------------------------------*/