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