]> andersk Git - openssh.git/blame - openbsd-compat/port-tun.c
- (djm) [loginrec.c ssh-rand-helper.c sshd.c openbsd-compat/glob.c]
[openssh.git] / openbsd-compat / port-tun.c
CommitLineData
e914f53a 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
22bbb3e6 19#include <sys/types.h>
3e9b2b1b 20#include <sys/ioctl.h>
21#include <netinet/ip.h>
22
22bbb3e6 23#include <fcntl.h>
24
e914f53a 25#include "log.h"
26#include "misc.h"
27#include "bufaux.h"
28
29/*
30 * This is the portable version of the SSH tunnel forwarding, it
31 * uses some preprocessor definitions for various platform-specific
32 * settings.
33 *
34 * SSH_TUN_LINUX Use the (newer) Linux tun/tap device
e02505e2 35 * SSH_TUN_FREEBSD Use the FreeBSD tun/tap device
e914f53a 36 * SSH_TUN_COMPAT_AF Translate the OpenBSD address family
37 * SSH_TUN_PREPEND_AF Prepend/remove the address family
38 */
39
40/*
41 * System-specific tunnel open function
42 */
43
44#if defined(SSH_TUN_LINUX)
3aef38da 45#include <linux/if.h>
e914f53a 46#include <linux/if_tun.h>
47
48int
49sys_tun_open(int tun, int mode)
50{
51 struct ifreq ifr;
52 int fd = -1;
53 const char *name = NULL;
54
55 if ((fd = open("/dev/net/tun", O_RDWR)) == -1) {
56 debug("%s: failed to open tunnel control interface: %s",
57 __func__, strerror(errno));
58 return (-1);
59 }
60
61 bzero(&ifr, sizeof(ifr));
62
63 if (mode == SSH_TUNMODE_ETHERNET) {
64 ifr.ifr_flags = IFF_TAP;
65 name = "tap%d";
66 } else {
67 ifr.ifr_flags = IFF_TUN;
68 name = "tun%d";
69 }
70 ifr.ifr_flags |= IFF_NO_PI;
71
72 if (tun != SSH_TUNID_ANY) {
73 if (tun > SSH_TUNID_MAX) {
74 debug("%s: invalid tunnel id %x: %s", __func__,
75 tun, strerror(errno));
76 goto failed;
77 }
78 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), name, tun);
79 }
80
81 if (ioctl(fd, TUNSETIFF, &ifr) == -1) {
82 debug("%s: failed to configure tunnel (mode %d): %s", __func__,
83 mode, strerror(errno));
84 goto failed;
85 }
86
87 if (tun == SSH_TUNID_ANY)
88 debug("%s: tunnel mode %d fd %d", __func__, mode, fd);
89 else
90 debug("%s: %s mode %d fd %d", __func__, ifr.ifr_name, mode, fd);
91
92 return (fd);
93
94 failed:
95 close(fd);
96 return (-1);
97}
98#endif /* SSH_TUN_LINUX */
99
0f6cb079 100#ifdef SSH_TUN_FREEBSD
101#include <sys/socket.h>
102#include <net/if.h>
e02505e2 103
104#ifdef HAVE_NET_IF_TUN_H
0f6cb079 105#include <net/if_tun.h>
e02505e2 106#endif
0f6cb079 107
108int
109sys_tun_open(int tun, int mode)
110{
111 struct ifreq ifr;
112 char name[100];
113 int fd = -1, sock, flag;
114 const char *tunbase = "tun";
115
116 if (mode == SSH_TUNMODE_ETHERNET) {
117#ifdef SSH_TUN_NO_L2
118 debug("%s: no layer 2 tunnelling support", __func__);
119 return (-1);
120#else
121 tunbase = "tap";
122#endif
123 }
124
125 /* Open the tunnel device */
126 if (tun <= SSH_TUNID_MAX) {
127 snprintf(name, sizeof(name), "/dev/%s%d", tunbase, tun);
128 fd = open(name, O_RDWR);
129 } else if (tun == SSH_TUNID_ANY) {
130 for (tun = 100; tun >= 0; tun--) {
131 snprintf(name, sizeof(name), "/dev/%s%d",
132 tunbase, tun);
133 if ((fd = open(name, O_RDWR)) >= 0)
134 break;
135 }
136 } else {
137 debug("%s: invalid tunnel %u\n", __func__, tun);
138 return (-1);
139 }
140
141 if (fd < 0) {
142 debug("%s: %s open failed: %s", __func__, name,
143 strerror(errno));
144 return (-1);
145 }
146
147 /* Turn on tunnel headers */
148 flag = 1;
149#if defined(TUNSIFHEAD) && !defined(SSH_TUN_PREPEND_AF)
150 if (mode != SSH_TUNMODE_ETHERNET &&
151 ioctl(fd, TUNSIFHEAD, &flag) == -1) {
152 debug("%s: ioctl(%d, TUNSIFHEAD, 1): %s", __func__, fd,
153 strerror(errno));
154 close(fd);
155 }
156#endif
157
158 debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
159
160 /* Set the tunnel device operation mode */
161 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", tunbase, tun);
162 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
163 goto failed;
164
165 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
166 goto failed;
167 ifr.ifr_flags |= IFF_UP;
168 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
169 goto failed;
170
171 close(sock);
172 return (fd);
173
174 failed:
175 if (fd >= 0)
176 close(fd);
177 if (sock >= 0)
178 close(sock);
179 debug("%s: failed to set %s mode %d: %s", __func__, name,
180 mode, strerror(errno));
181 return (-1);
182}
183#endif /* SSH_TUN_FREEBSD */
184
e914f53a 185/*
186 * System-specific channel filters
187 */
188
189#if defined(SSH_TUN_FILTER)
190#define OPENBSD_AF_INET 2
191#define OPENBSD_AF_INET6 24
192
193int
194sys_tun_infilter(struct Channel *c, char *buf, int len)
195{
196#if defined(SSH_TUN_PREPEND_AF)
197 char rbuf[CHAN_RBUF];
0f6cb079 198 struct ip *iph;
e914f53a 199#endif
200 u_int32_t *af;
201 char *ptr = buf;
202
203#if defined(SSH_TUN_PREPEND_AF)
0f6cb079 204 if (len <= 0 || len > (int)(sizeof(rbuf) - sizeof(*af)))
e914f53a 205 return (-1);
206 ptr = (char *)&rbuf[0];
207 bcopy(buf, ptr + sizeof(u_int32_t), len);
208 len += sizeof(u_int32_t);
0f6cb079 209 af = (u_int32_t *)ptr;
210
211 iph = (struct ip *)(ptr + sizeof(u_int32_t));
212 switch (iph->ip_v) {
213 case 6:
214 *af = AF_INET6;
215 break;
216 case 4:
217 default:
218 *af = AF_INET;
219 break;
220 }
e914f53a 221#endif
222
223#if defined(SSH_TUN_COMPAT_AF)
224 if (len < (int)sizeof(u_int32_t))
225 return (-1);
226
227 af = (u_int32_t *)ptr;
228 if (*af == htonl(AF_INET6))
229 *af = htonl(OPENBSD_AF_INET6);
230 else
231 *af = htonl(OPENBSD_AF_INET);
232#endif
0f6cb079 233
e914f53a 234 buffer_put_string(&c->input, ptr, len);
235 return (0);
236}
237
238u_char *
239sys_tun_outfilter(struct Channel *c, u_char **data, u_int *dlen)
240{
241 u_char *buf;
242 u_int32_t *af;
243
244 *data = buffer_get_string(&c->output, dlen);
245 if (*dlen < sizeof(*af))
246 return (NULL);
247 buf = *data;
248
249#if defined(SSH_TUN_PREPEND_AF)
250 *dlen -= sizeof(u_int32_t);
251 buf = *data + sizeof(u_int32_t);
252#elif defined(SSH_TUN_COMPAT_AF)
253 af = ntohl(*(u_int32_t *)buf);
254 if (*af == OPENBSD_AF_INET6)
255 *af = htonl(AF_INET6);
256 else
257 *af = htonl(AF_INET);
258#endif
259
260 return (buf);
261}
262#endif /* SSH_TUN_FILTER */
This page took 0.094456 seconds and 5 git commands to generate.