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