2 * Copyright (c) 2005 Reyk Floeter <reyk@openbsd.org>
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.
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.
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <netinet/ip.h>
32 #include "openbsd-compat/sys-queue.h"
39 * This is the portable version of the SSH tunnel forwarding, it
40 * uses some preprocessor definitions for various platform-specific
43 * SSH_TUN_LINUX Use the (newer) Linux tun/tap device
44 * SSH_TUN_FREEBSD Use the FreeBSD tun/tap device
45 * SSH_TUN_COMPAT_AF Translate the OpenBSD address family
46 * SSH_TUN_PREPEND_AF Prepend/remove the address family
50 * System-specific tunnel open function
53 #if defined(SSH_TUN_LINUX)
55 #include <linux/if_tun.h>
58 sys_tun_open(int tun
, int mode
)
62 const char *name
= NULL
;
64 if ((fd
= open("/dev/net/tun", O_RDWR
)) == -1) {
65 debug("%s: failed to open tunnel control interface: %s",
66 __func__
, strerror(errno
));
70 bzero(&ifr
, sizeof(ifr
));
72 if (mode
== SSH_TUNMODE_ETHERNET
) {
73 ifr
.ifr_flags
= IFF_TAP
;
76 ifr
.ifr_flags
= IFF_TUN
;
79 ifr
.ifr_flags
|= IFF_NO_PI
;
81 if (tun
!= SSH_TUNID_ANY
) {
82 if (tun
> SSH_TUNID_MAX
) {
83 debug("%s: invalid tunnel id %x: %s", __func__
,
84 tun
, strerror(errno
));
87 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), name
, tun
);
90 if (ioctl(fd
, TUNSETIFF
, &ifr
) == -1) {
91 debug("%s: failed to configure tunnel (mode %d): %s", __func__
,
92 mode
, strerror(errno
));
96 if (tun
== SSH_TUNID_ANY
)
97 debug("%s: tunnel mode %d fd %d", __func__
, mode
, fd
);
99 debug("%s: %s mode %d fd %d", __func__
, ifr
.ifr_name
, mode
, fd
);
107 #endif /* SSH_TUN_LINUX */
109 #ifdef SSH_TUN_FREEBSD
110 #include <sys/socket.h>
113 #ifdef HAVE_NET_IF_TUN_H
114 #include <net/if_tun.h>
118 sys_tun_open(int tun
, int mode
)
122 int fd
= -1, sock
, flag
;
123 const char *tunbase
= "tun";
125 if (mode
== SSH_TUNMODE_ETHERNET
) {
127 debug("%s: no layer 2 tunnelling support", __func__
);
134 /* Open the tunnel device */
135 if (tun
<= SSH_TUNID_MAX
) {
136 snprintf(name
, sizeof(name
), "/dev/%s%d", tunbase
, tun
);
137 fd
= open(name
, O_RDWR
);
138 } else if (tun
== SSH_TUNID_ANY
) {
139 for (tun
= 100; tun
>= 0; tun
--) {
140 snprintf(name
, sizeof(name
), "/dev/%s%d",
142 if ((fd
= open(name
, O_RDWR
)) >= 0)
146 debug("%s: invalid tunnel %u\n", __func__
, tun
);
151 debug("%s: %s open failed: %s", __func__
, name
,
156 /* Turn on tunnel headers */
158 #if defined(TUNSIFHEAD) && !defined(SSH_TUN_PREPEND_AF)
159 if (mode
!= SSH_TUNMODE_ETHERNET
&&
160 ioctl(fd
, TUNSIFHEAD
, &flag
) == -1) {
161 debug("%s: ioctl(%d, TUNSIFHEAD, 1): %s", __func__
, fd
,
167 debug("%s: %s mode %d fd %d", __func__
, name
, mode
, fd
);
169 /* Set the tunnel device operation mode */
170 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s%d", tunbase
, tun
);
171 if ((sock
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1)
174 if (ioctl(sock
, SIOCGIFFLAGS
, &ifr
) == -1)
176 if ((ifr
.ifr_flags
& IFF_UP
) == 0) {
177 ifr
.ifr_flags
|= IFF_UP
;
178 if (ioctl(sock
, SIOCSIFFLAGS
, &ifr
) == -1)
190 debug("%s: failed to set %s mode %d: %s", __func__
, name
,
191 mode
, strerror(errno
));
194 #endif /* SSH_TUN_FREEBSD */
197 * System-specific channel filters
200 #if defined(SSH_TUN_FILTER)
201 #define OPENBSD_AF_INET 2
202 #define OPENBSD_AF_INET6 24
205 sys_tun_infilter(struct Channel
*c
, char *buf
, int len
)
207 #if defined(SSH_TUN_PREPEND_AF)
208 char rbuf
[CHAN_RBUF
];
214 #if defined(SSH_TUN_PREPEND_AF)
215 if (len
<= 0 || len
> (int)(sizeof(rbuf
) - sizeof(*af
)))
217 ptr
= (char *)&rbuf
[0];
218 bcopy(buf
, ptr
+ sizeof(u_int32_t
), len
);
219 len
+= sizeof(u_int32_t
);
220 af
= (u_int32_t
*)ptr
;
222 iph
= (struct ip
*)(ptr
+ sizeof(u_int32_t
));
234 #if defined(SSH_TUN_COMPAT_AF)
235 if (len
< (int)sizeof(u_int32_t
))
238 af
= (u_int32_t
*)ptr
;
239 if (*af
== htonl(AF_INET6
))
240 *af
= htonl(OPENBSD_AF_INET6
);
242 *af
= htonl(OPENBSD_AF_INET
);
245 buffer_put_string(&c
->input
, ptr
, len
);
250 sys_tun_outfilter(struct Channel
*c
, u_char
**data
, u_int
*dlen
)
255 *data
= buffer_get_string(&c
->output
, dlen
);
256 if (*dlen
< sizeof(*af
))
260 #if defined(SSH_TUN_PREPEND_AF)
261 *dlen
-= sizeof(u_int32_t
);
262 buf
= *data
+ sizeof(u_int32_t
);
263 #elif defined(SSH_TUN_COMPAT_AF)
264 af
= ntohl(*(u_int32_t
*)buf
);
265 if (*af
== OPENBSD_AF_INET6
)
266 *af
= htonl(AF_INET6
);
268 *af
= htonl(AF_INET
);
273 #endif /* SSH_TUN_FILTER */