diff options
Diffstat (limited to 'openbsd-compat/port-net.c')
-rw-r--r-- | openbsd-compat/port-net.c | 291 |
1 files changed, 291 insertions, 0 deletions
diff --git a/openbsd-compat/port-net.c b/openbsd-compat/port-net.c new file mode 100644 index 000000000..0e75c911d --- /dev/null +++ b/openbsd-compat/port-net.c | |||
@@ -0,0 +1,291 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2005 Reyk Floeter <reyk@openbsd.org> | ||
3 | * | ||
4 | * Permission to use, copy, modify, and distribute this software for any | ||
5 | * purpose with or without fee is hereby granted, provided that the above | ||
6 | * copyright notice and this permission notice appear in all copies. | ||
7 | * | ||
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
15 | */ | ||
16 | |||
17 | #include "includes.h" | ||
18 | |||
19 | #include <sys/types.h> | ||
20 | #include <sys/ioctl.h> | ||
21 | |||
22 | #include <netinet/in.h> | ||
23 | #include <arpa/inet.h> | ||
24 | #include <netinet/ip.h> | ||
25 | |||
26 | #include <errno.h> | ||
27 | #include <fcntl.h> | ||
28 | #include <stdarg.h> | ||
29 | #include <string.h> | ||
30 | #include <unistd.h> | ||
31 | |||
32 | #include "openbsd-compat/sys-queue.h" | ||
33 | #include "log.h" | ||
34 | #include "misc.h" | ||
35 | #include "sshbuf.h" | ||
36 | #include "channels.h" | ||
37 | #include "ssherr.h" | ||
38 | |||
39 | /* | ||
40 | * This is the portable version of the SSH tunnel forwarding, it | ||
41 | * uses some preprocessor definitions for various platform-specific | ||
42 | * settings. | ||
43 | * | ||
44 | * SSH_TUN_LINUX Use the (newer) Linux tun/tap device | ||
45 | * SSH_TUN_FREEBSD Use the FreeBSD tun/tap device | ||
46 | * SSH_TUN_COMPAT_AF Translate the OpenBSD address family | ||
47 | * SSH_TUN_PREPEND_AF Prepend/remove the address family | ||
48 | */ | ||
49 | |||
50 | /* | ||
51 | * System-specific tunnel open function | ||
52 | */ | ||
53 | |||
54 | #if defined(SSH_TUN_LINUX) | ||
55 | #include <linux/if.h> | ||
56 | #include <linux/if_tun.h> | ||
57 | |||
58 | int | ||
59 | sys_tun_open(int tun, int mode, char **ifname) | ||
60 | { | ||
61 | struct ifreq ifr; | ||
62 | int fd = -1; | ||
63 | const char *name = NULL; | ||
64 | |||
65 | if (ifname != NULL) | ||
66 | *ifname = NULL; | ||
67 | |||
68 | if ((fd = open("/dev/net/tun", O_RDWR)) == -1) { | ||
69 | debug("%s: failed to open tunnel control interface: %s", | ||
70 | __func__, strerror(errno)); | ||
71 | return (-1); | ||
72 | } | ||
73 | |||
74 | bzero(&ifr, sizeof(ifr)); | ||
75 | |||
76 | if (mode == SSH_TUNMODE_ETHERNET) { | ||
77 | ifr.ifr_flags = IFF_TAP; | ||
78 | name = "tap%d"; | ||
79 | } else { | ||
80 | ifr.ifr_flags = IFF_TUN; | ||
81 | name = "tun%d"; | ||
82 | } | ||
83 | ifr.ifr_flags |= IFF_NO_PI; | ||
84 | |||
85 | if (tun != SSH_TUNID_ANY) { | ||
86 | if (tun > SSH_TUNID_MAX) { | ||
87 | debug("%s: invalid tunnel id %x: %s", __func__, | ||
88 | tun, strerror(errno)); | ||
89 | goto failed; | ||
90 | } | ||
91 | snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), name, tun); | ||
92 | } | ||
93 | |||
94 | if (ioctl(fd, TUNSETIFF, &ifr) == -1) { | ||
95 | debug("%s: failed to configure tunnel (mode %d): %s", __func__, | ||
96 | mode, strerror(errno)); | ||
97 | goto failed; | ||
98 | } | ||
99 | |||
100 | if (tun == SSH_TUNID_ANY) | ||
101 | debug("%s: tunnel mode %d fd %d", __func__, mode, fd); | ||
102 | else | ||
103 | debug("%s: %s mode %d fd %d", __func__, ifr.ifr_name, mode, fd); | ||
104 | |||
105 | if (ifname != NULL && (*ifname = strdup(ifr.ifr_name))) | ||
106 | goto failed; | ||
107 | |||
108 | return (fd); | ||
109 | |||
110 | failed: | ||
111 | close(fd); | ||
112 | return (-1); | ||
113 | } | ||
114 | #endif /* SSH_TUN_LINUX */ | ||
115 | |||
116 | #ifdef SSH_TUN_FREEBSD | ||
117 | #include <sys/socket.h> | ||
118 | #include <net/if.h> | ||
119 | |||
120 | #ifdef HAVE_NET_IF_TUN_H | ||
121 | #include <net/if_tun.h> | ||
122 | #endif | ||
123 | |||
124 | int | ||
125 | sys_tun_open(int tun, int mode, char **ifname) | ||
126 | { | ||
127 | struct ifreq ifr; | ||
128 | char name[100]; | ||
129 | int fd = -1, sock, flag; | ||
130 | const char *tunbase = "tun"; | ||
131 | |||
132 | if (ifname != NULL) | ||
133 | *ifname = NULL; | ||
134 | |||
135 | if (mode == SSH_TUNMODE_ETHERNET) { | ||
136 | #ifdef SSH_TUN_NO_L2 | ||
137 | debug("%s: no layer 2 tunnelling support", __func__); | ||
138 | return (-1); | ||
139 | #else | ||
140 | tunbase = "tap"; | ||
141 | #endif | ||
142 | } | ||
143 | |||
144 | /* Open the tunnel device */ | ||
145 | if (tun <= SSH_TUNID_MAX) { | ||
146 | snprintf(name, sizeof(name), "/dev/%s%d", tunbase, tun); | ||
147 | fd = open(name, O_RDWR); | ||
148 | } else if (tun == SSH_TUNID_ANY) { | ||
149 | for (tun = 100; tun >= 0; tun--) { | ||
150 | snprintf(name, sizeof(name), "/dev/%s%d", | ||
151 | tunbase, tun); | ||
152 | if ((fd = open(name, O_RDWR)) >= 0) | ||
153 | break; | ||
154 | } | ||
155 | } else { | ||
156 | debug("%s: invalid tunnel %u\n", __func__, tun); | ||
157 | return (-1); | ||
158 | } | ||
159 | |||
160 | if (fd < 0) { | ||
161 | debug("%s: %s open failed: %s", __func__, name, | ||
162 | strerror(errno)); | ||
163 | return (-1); | ||
164 | } | ||
165 | |||
166 | /* Turn on tunnel headers */ | ||
167 | flag = 1; | ||
168 | #if defined(TUNSIFHEAD) && !defined(SSH_TUN_PREPEND_AF) | ||
169 | if (mode != SSH_TUNMODE_ETHERNET && | ||
170 | ioctl(fd, TUNSIFHEAD, &flag) == -1) { | ||
171 | debug("%s: ioctl(%d, TUNSIFHEAD, 1): %s", __func__, fd, | ||
172 | strerror(errno)); | ||
173 | close(fd); | ||
174 | } | ||
175 | #endif | ||
176 | |||
177 | debug("%s: %s mode %d fd %d", __func__, name, mode, fd); | ||
178 | |||
179 | /* Set the tunnel device operation mode */ | ||
180 | snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", tunbase, tun); | ||
181 | if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) | ||
182 | goto failed; | ||
183 | |||
184 | if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) | ||
185 | goto failed; | ||
186 | if ((ifr.ifr_flags & IFF_UP) == 0) { | ||
187 | ifr.ifr_flags |= IFF_UP; | ||
188 | if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) | ||
189 | goto failed; | ||
190 | } | ||
191 | |||
192 | if (ifname != NULL && (*ifname = strdup(ifr.ifr_name))) | ||
193 | goto failed; | ||
194 | |||
195 | close(sock); | ||
196 | return (fd); | ||
197 | |||
198 | failed: | ||
199 | if (fd >= 0) | ||
200 | close(fd); | ||
201 | if (sock >= 0) | ||
202 | close(sock); | ||
203 | debug("%s: failed to set %s mode %d: %s", __func__, name, | ||
204 | mode, strerror(errno)); | ||
205 | return (-1); | ||
206 | } | ||
207 | #endif /* SSH_TUN_FREEBSD */ | ||
208 | |||
209 | /* | ||
210 | * System-specific channel filters | ||
211 | */ | ||
212 | |||
213 | #if defined(SSH_TUN_FILTER) | ||
214 | /* | ||
215 | * The tunnel forwarding protocol prepends the address family of forwarded | ||
216 | * IP packets using OpenBSD's numbers. | ||
217 | */ | ||
218 | #define OPENBSD_AF_INET 2 | ||
219 | #define OPENBSD_AF_INET6 24 | ||
220 | |||
221 | int | ||
222 | sys_tun_infilter(struct ssh *ssh, struct Channel *c, char *buf, int _len) | ||
223 | { | ||
224 | int r; | ||
225 | size_t len; | ||
226 | char *ptr = buf; | ||
227 | #if defined(SSH_TUN_PREPEND_AF) | ||
228 | char rbuf[CHAN_RBUF]; | ||
229 | struct ip iph; | ||
230 | #endif | ||
231 | #if defined(SSH_TUN_PREPEND_AF) || defined(SSH_TUN_COMPAT_AF) | ||
232 | u_int32_t af; | ||
233 | #endif | ||
234 | |||
235 | /* XXX update channel input filter API to use unsigned length */ | ||
236 | if (_len < 0) | ||
237 | return -1; | ||
238 | len = _len; | ||
239 | |||
240 | #if defined(SSH_TUN_PREPEND_AF) | ||
241 | if (len <= sizeof(iph) || len > sizeof(rbuf) - 4) | ||
242 | return -1; | ||
243 | /* Determine address family from packet IP header. */ | ||
244 | memcpy(&iph, buf, sizeof(iph)); | ||
245 | af = iph.ip_v == 6 ? OPENBSD_AF_INET6 : OPENBSD_AF_INET; | ||
246 | /* Prepend address family to packet using OpenBSD constants */ | ||
247 | memcpy(rbuf + 4, buf, len); | ||
248 | len += 4; | ||
249 | POKE_U32(rbuf, af); | ||
250 | ptr = rbuf; | ||
251 | #elif defined(SSH_TUN_COMPAT_AF) | ||
252 | /* Convert existing address family header to OpenBSD value */ | ||
253 | if (len <= 4) | ||
254 | return -1; | ||
255 | af = PEEK_U32(buf); | ||
256 | /* Put it back */ | ||
257 | POKE_U32(buf, af == AF_INET6 ? OPENBSD_AF_INET6 : OPENBSD_AF_INET); | ||
258 | #endif | ||
259 | |||
260 | if ((r = sshbuf_put_string(c->input, ptr, len)) != 0) | ||
261 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); | ||
262 | return (0); | ||
263 | } | ||
264 | |||
265 | u_char * | ||
266 | sys_tun_outfilter(struct ssh *ssh, struct Channel *c, | ||
267 | u_char **data, size_t *dlen) | ||
268 | { | ||
269 | u_char *buf; | ||
270 | u_int32_t af; | ||
271 | int r; | ||
272 | |||
273 | /* XXX new API is incompatible with this signature. */ | ||
274 | if ((r = sshbuf_get_string(c->output, data, dlen)) != 0) | ||
275 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); | ||
276 | if (*dlen < sizeof(af)) | ||
277 | return (NULL); | ||
278 | buf = *data; | ||
279 | |||
280 | #if defined(SSH_TUN_PREPEND_AF) | ||
281 | /* skip address family */ | ||
282 | *dlen -= sizeof(af); | ||
283 | buf = *data + sizeof(af); | ||
284 | #elif defined(SSH_TUN_COMPAT_AF) | ||
285 | /* translate address family */ | ||
286 | af = (PEEK_U32(buf) == OPENBSD_AF_INET6) ? AF_INET6 : AF_INET; | ||
287 | POKE_U32(buf, af); | ||
288 | #endif | ||
289 | return (buf); | ||
290 | } | ||
291 | #endif /* SSH_TUN_FILTER */ | ||