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