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