410c8acf |
1 | /* -*-c-*- |
2 | * |
3cdc3f3a |
3 | * $Id$ |
410c8acf |
4 | * |
5 | * Tunnel interface based on Linux Usernet |
6 | * |
7 | * (c) 2001 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
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. |
18 | * |
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. |
23 | * |
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 | |
45 | struct 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 | |
51 | /* --- @t_ifname@ --- * |
52 | * |
53 | * Arguments: @tunnel *t@ = pointer to tunnel block |
54 | * |
55 | * Returns: A pointer to the tunnel's interface name. |
56 | */ |
57 | |
58 | static const char *t_ifname(tunnel *t) |
59 | { |
60 | static char b[UNET_NAMEMAX]; |
61 | struct unet_info uni; |
62 | if (ioctl(t->f.fd, UNIOCGINFO, &uni)) { |
f43df819 |
63 | a_warn("TUN", "-", "unet", "getinfo-error", "?ERRNO", A_END); |
42da2a58 |
64 | return ("<error>"); |
65 | } |
66 | if (strlen(uni.uni_ifname) + 1 > sizeof(b)) { |
f43df819 |
67 | a_warn("TUN", "-", "unet", "ifname-too-long", A_END); |
42da2a58 |
68 | return ("<error>"); |
69 | } |
70 | strcpy(b, uni.uni_ifname); |
71 | return (b); |
72 | } |
e78f24a2 |
73 | |
410c8acf |
74 | /* --- @t_read@ --- * |
75 | * |
76 | * Arguments: @int fd@ = file descriptor to read |
77 | * @unsigned mode@ = what's happened |
78 | * @void *v@ = pointer to tunnel block |
79 | * |
80 | * Returns: --- |
81 | * |
82 | * Use: Reads data from the tunnel. |
83 | */ |
84 | |
60a837d8 |
85 | static void t_read(int fd, unsigned mode, void *v) |
410c8acf |
86 | { |
87 | tunnel *t = v; |
88 | ssize_t n; |
89 | buf b; |
90 | |
91 | n = read(fd, buf_i, sizeof(buf_i)); |
92 | if (n < 0) { |
f43df819 |
93 | a_warn("TUN", "%s", t_ifname(t), "read-error", "?ERRNO", A_END); |
410c8acf |
94 | return; |
95 | } |
96 | IF_TRACING(T_TUNNEL, { |
060ca767 |
97 | trace(T_TUNNEL, "tun-unet: packet arrived"); |
98 | trace_block(T_PACKET, "tun-unet: packet contents", buf_i, n); |
410c8acf |
99 | }) |
100 | buf_init(&b, buf_i, n); |
101 | p_tun(t->p, &b); |
102 | } |
103 | |
42da2a58 |
104 | /* --- @t_init@ --- * |
410c8acf |
105 | * |
106 | * Arguments: --- |
107 | * |
108 | * Returns: --- |
109 | * |
110 | * Use: Initializes the tunneling system. Maybe this will require |
111 | * opening file descriptors or something. |
112 | */ |
113 | |
42da2a58 |
114 | static void t_init(void) { return; } |
410c8acf |
115 | |
42da2a58 |
116 | /* --- @t_create@ --- * |
410c8acf |
117 | * |
118 | * Arguments: @tunnel *t@ = pointer to tunnel block |
119 | * @peer *p@ = pointer to peer block |
120 | * |
42da2a58 |
121 | * Returns: A tunnel block if it worked, or null on failure. |
410c8acf |
122 | * |
123 | * Use: Initializes a new tunnel. |
124 | */ |
125 | |
42da2a58 |
126 | static tunnel *t_create(peer *p) |
410c8acf |
127 | { |
128 | int fd; |
42da2a58 |
129 | tunnel *t; |
a368bfbc |
130 | int f; |
410c8acf |
131 | |
132 | if ((fd = open("/dev/unet", O_RDWR)) < 0) { |
f43df819 |
133 | a_warn("TUN", "-", "unet", "open-error", "/dev/unet", "?ERRNO", A_END); |
42da2a58 |
134 | return (0); |
410c8acf |
135 | } |
650a6624 |
136 | fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
a368bfbc |
137 | if ((f = ioctl(fd, UNIOCGIFFLAGS)) < 0 || |
138 | ioctl(fd, UNIOCSIFFLAGS, f | IFF_POINTOPOINT)) { |
f43df819 |
139 | a_warn("TUN", "-", "unet", "config-error", "?ERRNO", A_END); |
a368bfbc |
140 | close(fd); |
42da2a58 |
141 | return (0); |
a368bfbc |
142 | } |
42da2a58 |
143 | t = CREATE(tunnel); |
144 | t->ops = &tun_unet; |
410c8acf |
145 | t->p = p; |
146 | sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t); |
147 | sel_addfile(&t->f); |
060ca767 |
148 | T( trace(T_TUNNEL, "tun-unet: attached interface %s to peer `%s'", |
42da2a58 |
149 | t_ifname(t), p_name(p)); ) |
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 |
163 | static 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 |
181 | static void t_destroy(tunnel *t) |
410c8acf |
182 | { |
183 | sel_rmfile(&t->f); |
184 | close(t->f.fd); |
42da2a58 |
185 | DESTROY(t); |
410c8acf |
186 | } |
187 | |
42da2a58 |
188 | const tunnel_ops tun_unet = { |
189 | "unet", |
190 | t_init, |
191 | t_create, |
192 | t_ifname, |
193 | t_inject, |
194 | t_destroy |
195 | }; |
196 | |
197 | #endif |
198 | |
410c8acf |
199 | /*----- That's all, folks -------------------------------------------------*/ |