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