OpenSSH: Revert few local modifications.
[dragonfly.git] / crypto / openssh / openbsd-compat / port-tun.c
CommitLineData
16c343f1
PA
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
c8c467ee 32#include "openbsd-compat/sys-queue.h"
16c343f1
PA
33#include "log.h"
34#include "misc.h"
e9778795 35#include "sshbuf.h"
16c343f1 36#include "channels.h"
e9778795 37#include "ssherr.h"
16c343f1
PA
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
58int
59sys_tun_open(int tun, int mode)
60{
61 struct ifreq ifr;
62 int fd = -1;
63 const char *name = NULL;
64
65 if ((fd = open("/dev/net/tun", O_RDWR)) == -1) {
66 debug("%s: failed to open tunnel control interface: %s",
67 __func__, strerror(errno));
68 return (-1);
69 }
70
2c9c1408 71 bzero(&ifr, sizeof(ifr));
16c343f1
PA
72
73 if (mode == SSH_TUNMODE_ETHERNET) {
74 ifr.ifr_flags = IFF_TAP;
75 name = "tap%d";
76 } else {
77 ifr.ifr_flags = IFF_TUN;
78 name = "tun%d";
79 }
80 ifr.ifr_flags |= IFF_NO_PI;
81
82 if (tun != SSH_TUNID_ANY) {
83 if (tun > SSH_TUNID_MAX) {
84 debug("%s: invalid tunnel id %x: %s", __func__,
85 tun, strerror(errno));
86 goto failed;
87 }
88 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), name, tun);
89 }
90
91 if (ioctl(fd, TUNSETIFF, &ifr) == -1) {
92 debug("%s: failed to configure tunnel (mode %d): %s", __func__,
93 mode, strerror(errno));
94 goto failed;
95 }
96
97 if (tun == SSH_TUNID_ANY)
98 debug("%s: tunnel mode %d fd %d", __func__, mode, fd);
99 else
100 debug("%s: %s mode %d fd %d", __func__, ifr.ifr_name, mode, fd);
101
102 return (fd);
103
104 failed:
105 close(fd);
106 return (-1);
107}
108#endif /* SSH_TUN_LINUX */
109
110#ifdef SSH_TUN_FREEBSD
111#include <sys/socket.h>
112#include <net/if.h>
113
114#ifdef HAVE_NET_IF_TUN_H
a4a3adb1 115#include <net/if_tun.h>
16c343f1
PA
116#endif
117
118int
119sys_tun_open(int tun, int mode)
120{
121 struct ifreq ifr;
122 char name[100];
123 int fd = -1, sock, flag;
124 const char *tunbase = "tun";
125
126 if (mode == SSH_TUNMODE_ETHERNET) {
127#ifdef SSH_TUN_NO_L2
128 debug("%s: no layer 2 tunnelling support", __func__);
129 return (-1);
130#else
131 tunbase = "tap";
132#endif
133 }
134
135 /* Open the tunnel device */
136 if (tun <= SSH_TUNID_MAX) {
137 snprintf(name, sizeof(name), "/dev/%s%d", tunbase, tun);
138 fd = open(name, O_RDWR);
139 } else if (tun == SSH_TUNID_ANY) {
140 for (tun = 100; tun >= 0; tun--) {
141 snprintf(name, sizeof(name), "/dev/%s%d",
142 tunbase, tun);
143 if ((fd = open(name, O_RDWR)) >= 0)
144 break;
145 }
146 } else {
147 debug("%s: invalid tunnel %u\n", __func__, tun);
148 return (-1);
149 }
150
151 if (fd < 0) {
152 debug("%s: %s open failed: %s", __func__, name,
153 strerror(errno));
154 return (-1);
155 }
156
157 /* Turn on tunnel headers */
158 flag = 1;
159#if defined(TUNSIFHEAD) && !defined(SSH_TUN_PREPEND_AF)
160 if (mode != SSH_TUNMODE_ETHERNET &&
161 ioctl(fd, TUNSIFHEAD, &flag) == -1) {
162 debug("%s: ioctl(%d, TUNSIFHEAD, 1): %s", __func__, fd,
163 strerror(errno));
164 close(fd);
165 }
166#endif
167
168 debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
169
170 /* Set the tunnel device operation mode */
171 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", tunbase, tun);
172 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
173 goto failed;
174
175 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
176 goto failed;
856ea928
PA
177 if ((ifr.ifr_flags & IFF_UP) == 0) {
178 ifr.ifr_flags |= IFF_UP;
179 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
180 goto failed;
181 }
16c343f1
PA
182
183 close(sock);
184 return (fd);
185
186 failed:
187 if (fd >= 0)
188 close(fd);
189 if (sock >= 0)
190 close(sock);
191 debug("%s: failed to set %s mode %d: %s", __func__, name,
192 mode, strerror(errno));
193 return (-1);
194}
195#endif /* SSH_TUN_FREEBSD */
196
197/*
198 * System-specific channel filters
199 */
200
201#if defined(SSH_TUN_FILTER)
2c9c1408
MD
202/*
203 * The tunnel forwarding protocol prepends the address family of forwarded
204 * IP packets using OpenBSD's numbers.
205 */
16c343f1
PA
206#define OPENBSD_AF_INET 2
207#define OPENBSD_AF_INET6 24
208
209int
2c9c1408 210sys_tun_infilter(struct ssh *ssh, struct Channel *c, char *buf, int _len)
16c343f1 211{
2c9c1408
MD
212 int r;
213 size_t len;
214 char *ptr = buf;
16c343f1
PA
215#if defined(SSH_TUN_PREPEND_AF)
216 char rbuf[CHAN_RBUF];
2c9c1408 217 struct ip iph;
16c343f1 218#endif
2c9c1408
MD
219#if defined(SSH_TUN_PREPEND_AF) || defined(SSH_TUN_COMPAT_AF)
220 u_int32_t af;
16c343f1
PA
221#endif
222
2c9c1408
MD
223 /* XXX update channel input filter API to use unsigned length */
224 if (_len < 0)
225 return -1;
226 len = _len;
16c343f1 227
2c9c1408
MD
228#if defined(SSH_TUN_PREPEND_AF)
229 if (len <= sizeof(iph) || len > sizeof(rbuf) - 4)
230 return -1;
231 /* Determine address family from packet IP header. */
232 memcpy(&iph, buf, sizeof(iph));
233 af = iph.ip_v == 6 ? OPENBSD_AF_INET6 : OPENBSD_AF_INET;
234 /* Prepend address family to packet using OpenBSD constants */
235 memcpy(rbuf + 4, buf, len);
236 len += 4;
237 POKE_U32(rbuf, af);
238 ptr = rbuf;
239#elif defined(SSH_TUN_COMPAT_AF)
240 /* Convert existing address family header to OpenBSD value */
241 if (len <= 4)
242 return -1;
243 af = PEEK_U32(buf);
244 /* Put it back */
245 POKE_U32(buf, af == AF_INET6 ? OPENBSD_AF_INET6 : OPENBSD_AF_INET);
16c343f1
PA
246#endif
247
2c9c1408 248 if ((r = sshbuf_put_string(c->input, ptr, len)) != 0)
e9778795 249 fatal("%s: buffer error: %s", __func__, ssh_err(r));
16c343f1
PA
250 return (0);
251}
252
253u_char *
2c9c1408
MD
254sys_tun_outfilter(struct ssh *ssh, struct Channel *c,
255 u_char **data, size_t *dlen)
16c343f1
PA
256{
257 u_char *buf;
2c9c1408 258 u_int32_t af;
e9778795 259 int r;
e9778795
PA
260
261 /* XXX new API is incompatible with this signature. */
2c9c1408 262 if ((r = sshbuf_get_string(c->output, data, dlen)) != 0)
e9778795 263 fatal("%s: buffer error: %s", __func__, ssh_err(r));
2c9c1408 264 if (*dlen < sizeof(af))
16c343f1
PA
265 return (NULL);
266 buf = *data;
267
268#if defined(SSH_TUN_PREPEND_AF)
2c9c1408
MD
269 /* skip address family */
270 *dlen -= sizeof(af);
271 buf = *data + sizeof(af);
16c343f1 272#elif defined(SSH_TUN_COMPAT_AF)
2c9c1408
MD
273 /* translate address family */
274 af = (PEEK_U32(buf) == OPENBSD_AF_INET6) ? AF_INET6 : AF_INET;
275 POKE_U32(buf, af);
16c343f1 276#endif
16c343f1
PA
277 return (buf);
278}
279#endif /* SSH_TUN_FILTER */