fd528bde |
1 | /* -*-c-*- |
2 | * |
3cdc3f3a |
3 | * $Id$ |
fd528bde |
4 | * |
5 | * Tunnel interface for 4.4BSD-derived systems |
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 | |
fd528bde |
29 | /*----- Header files ------------------------------------------------------*/ |
30 | |
42da2a58 |
31 | #define TUN_INTERNALS |
32 | |
fd528bde |
33 | #include "tripe.h" |
34 | |
35 | /*----- Main code ---------------------------------------------------------*/ |
36 | |
42da2a58 |
37 | #ifdef TUN_BSD |
38 | |
39 | struct tunnel { |
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 */ |
44 | }; |
45 | |
46 | /* --- @t_ifname@ --- * |
47 | * |
48 | * Arguments: @tunnel *t@ = pointer to tunnel block |
49 | * |
50 | * Returns: A pointer to the tunnel's interface name. |
51 | */ |
52 | |
53 | static const char *t_ifname(tunnel *t) |
54 | { |
55 | static char buf[8]; |
56 | sprintf(buf, "tun%u", t->n); |
57 | return (buf); |
58 | } |
fd528bde |
59 | |
60 | /* --- @t_read@ --- * |
61 | * |
62 | * Arguments: @int fd@ = file descriptor to read |
63 | * @unsigned mode@ = what's happened |
64 | * @void *v@ = pointer to tunnel block |
65 | * |
66 | * Returns: --- |
67 | * |
68 | * Use: Reads data from the tunnel. |
69 | */ |
70 | |
42da2a58 |
71 | static void t_read(int fd, unsigned mode, void *v) |
fd528bde |
72 | { |
73 | tunnel *t = v; |
74 | ssize_t n; |
75 | buf b; |
76 | |
77 | n = read(fd, buf_i, sizeof(buf_i)); |
78 | if (n < 0) { |
42da2a58 |
79 | a_warn("TUN %s read-error -- %s", t_ifname(t), strerror(errno)); |
fd528bde |
80 | return; |
81 | } |
82 | IF_TRACING(T_TUNNEL, { |
83 | trace(T_TUNNEL, "tunnel: packet arrived"); |
84 | trace_block(T_PACKET, "tunnel: packet contents", buf_i, n); |
85 | }) |
86 | buf_init(&b, buf_i, n); |
87 | p_tun(t->p, &b); |
88 | } |
89 | |
42da2a58 |
90 | /* --- @t_init@ --- * |
fd528bde |
91 | * |
92 | * Arguments: --- |
93 | * |
94 | * Returns: --- |
95 | * |
96 | * Use: Initializes the tunneling system. Maybe this will require |
97 | * opening file descriptors or something. |
98 | */ |
99 | |
42da2a58 |
100 | static void t_init(void) { return; } |
fd528bde |
101 | |
42da2a58 |
102 | /* --- @t_create@ --- * |
fd528bde |
103 | * |
42da2a58 |
104 | * Arguments: @peer *p@ = pointer to peer block |
fd528bde |
105 | * |
42da2a58 |
106 | * Returns: A tunnel block if it worked, or null on failure. |
fd528bde |
107 | * |
108 | * Use: Initializes a new tunnel. |
109 | */ |
110 | |
42da2a58 |
111 | static tunnel *t_create(peer *p) |
fd528bde |
112 | { |
113 | int fd; |
114 | unsigned n; |
42da2a58 |
115 | tunnel *t; |
fd528bde |
116 | char buf[16]; |
117 | |
118 | n = 0; |
119 | for (;;) { |
120 | sprintf(buf, "/dev/tun%u", n); |
ef4a1ab7 |
121 | if ((fd = open(buf, O_RDWR)) >= 0) |
fd528bde |
122 | break; |
123 | switch (errno) { |
124 | case EBUSY: |
125 | T( trace(T_TUNNEL, "tunnel device %u busy: skipping", n); ) |
126 | break; |
127 | case ENOENT: |
3cdc3f3a |
128 | a_warn("TUN - bsd no-tunnel-devices"); |
42da2a58 |
129 | return (0); |
fd528bde |
130 | default: |
3cdc3f3a |
131 | a_warn("TUN - open-error %s -- %s", buf, strerror(errno)); |
fd528bde |
132 | break; |
133 | } |
134 | n++; |
135 | } |
136 | |
42da2a58 |
137 | t = CREATE(tunnel); |
138 | t->ops = &tun_bsd; |
fd528bde |
139 | fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
140 | t->p = p; |
141 | t->n = n; |
142 | sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t); |
143 | sel_addfile(&t->f); |
144 | T( trace(T_TUNNEL, "tunnel: attached interface %s to peer `%s'", |
42da2a58 |
145 | t_ifname(t), p_name(p)); ) |
146 | return (t); |
fd528bde |
147 | } |
148 | |
42da2a58 |
149 | /* --- @t_inject@ --- * |
fd528bde |
150 | * |
151 | * Arguments: @tunnel *t@ = pointer to tunnel block |
152 | * @buf *b@ = buffer to send |
153 | * |
154 | * Returns: --- |
155 | * |
156 | * Use: Injects a packet into the local network stack. |
157 | */ |
158 | |
42da2a58 |
159 | static void t_inject(tunnel *t, buf *b) |
fd528bde |
160 | { |
161 | IF_TRACING(T_TUNNEL, { |
162 | trace(T_TUNNEL, "tunnel: inject decrypted packet"); |
163 | trace_block(T_PACKET, "tunnel: packet contents", BBASE(b), BLEN(b)); |
164 | }) |
165 | write(t->f.fd, BBASE(b), BLEN(b)); |
166 | } |
167 | |
42da2a58 |
168 | /* --- @t_destroy@ --- * |
fd528bde |
169 | * |
170 | * Arguments: @tunnel *t@ = pointer to tunnel block |
171 | * |
172 | * Returns: --- |
173 | * |
174 | * Use: Destroys a tunnel. |
175 | */ |
176 | |
42da2a58 |
177 | static void t_destroy(tunnel *t) |
fd528bde |
178 | { |
179 | sel_rmfile(&t->f); |
180 | close(t->f.fd); |
42da2a58 |
181 | DESTROY(t); |
fd528bde |
182 | } |
183 | |
42da2a58 |
184 | const tunnel_ops tun_bsd = { |
185 | "bsd", |
186 | t_init, |
187 | t_create, |
188 | t_ifname, |
189 | t_inject, |
190 | t_destroy |
191 | }; |
192 | |
193 | #endif |
194 | |
fd528bde |
195 | /*----- That's all, folks -------------------------------------------------*/ |