blob: bb535626ffe02062a7e87f6edb4d0680d23d8904 [file] [log] [blame]
Damien Miller598bbc22005-12-31 16:33:36 +11001/*
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
Damien Millera1738e42006-07-10 21:33:04 +100019#include <sys/types.h>
Tim Rice4b23f7c2006-03-14 22:09:50 -080020#include <sys/ioctl.h>
Darren Tucker46aa3e02006-09-02 15:32:40 +100021
Darren Tucker686852f2006-07-12 19:05:56 +100022#include <netinet/in.h>
Darren Tucker46aa3e02006-09-02 15:32:40 +100023#include <arpa/inet.h>
Tim Rice4b23f7c2006-03-14 22:09:50 -080024#include <netinet/ip.h>
25
Darren Tucker2c1a02a2006-07-12 22:40:50 +100026#include <errno.h>
Damien Millera1738e42006-07-10 21:33:04 +100027#include <fcntl.h>
Darren Tuckere7eec902006-08-05 15:47:26 +100028#include <stdarg.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100029#include <string.h>
30#include <unistd.h>
Damien Millera1738e42006-07-10 21:33:04 +100031
Damien Miller94178312008-05-19 15:28:35 +100032#include "openbsd-compat/sys-queue.h"
Damien Miller598bbc22005-12-31 16:33:36 +110033#include "log.h"
34#include "misc.h"
Damien Miller0cdc5a32015-01-15 02:35:33 +110035#include "sshbuf.h"
Damien Miller75bb6642006-08-05 14:07:20 +100036#include "channels.h"
Damien Miller0cdc5a32015-01-15 02:35:33 +110037#include "ssherr.h"
Damien Miller598bbc22005-12-31 16:33:36 +110038
39/*
Damien Miller6eee79f2017-10-25 13:22:29 +110040 * This file contains various portability code for network support,
41 * including tun/tap forwarding and routing domains.
42 */
43
Damien Millerce1cca32017-10-25 13:47:59 +110044#if defined(SYS_RDOMAIN_LINUX) || defined(SSH_TUN_LINUX)
45#include <linux/if.h>
46#endif
47
48#if defined(SYS_RDOMAIN_LINUX)
49char *
50sys_get_rdomain(int fd)
51{
52 char dev[IFNAMSIZ + 1];
53 socklen_t len = sizeof(dev) - 1;
54
55 if (getsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, dev, &len) == -1) {
56 error("%s: cannot determine VRF for fd=%d : %s",
57 __func__, fd, strerror(errno));
58 return NULL;
59 }
60 dev[len] = '\0';
61 return strdup(dev);
62}
63
64int
65sys_set_rdomain(int fd, const char *name)
66{
67 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
68 name, strlen(name)) == -1) {
69 error("%s: setsockopt(%d, SO_BINDTODEVICE, %s): %s",
70 __func__, fd, name, strerror(errno));
71 return -1;
72 }
73 return 0;
74}
75
76int
Damien Miller2de5c6b2017-10-27 08:42:33 +110077sys_valid_rdomain(const char *name)
Damien Millerce1cca32017-10-25 13:47:59 +110078{
Damien Miller97c5aaf2017-10-25 14:09:56 +110079 int fd;
80
81 /*
82 * This is a pretty crappy way to test. It would be better to
83 * check whether "name" represents a VRF device, but apparently
84 * that requires an rtnetlink transaction.
85 */
86 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
87 return 0;
88 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
89 name, strlen(name)) == -1) {
90 close(fd);
91 return 0;
92 }
93 close(fd);
94 return 1;
Damien Millerce1cca32017-10-25 13:47:59 +110095}
96#elif defined(SYS_RDOMAIN_XXX)
Damien Miller6eee79f2017-10-25 13:22:29 +110097/* XXX examples */
98char *
99sys_get_rdomain(int fd)
100{
101 return NULL;
102}
103
104int
105sys_set_rdomain(int fd, const char *name)
106{
107 return -1;
108}
109
110int
111valid_rdomain(const char *name)
112{
113 return 0;
114}
115
116void
117sys_set_process_rdomain(const char *name)
118{
119 fatal("%s: not supported", __func__);
120}
121#endif /* defined(SYS_RDOMAIN_XXX) */
122
123/*
Damien Miller598bbc22005-12-31 16:33:36 +1100124 * This is the portable version of the SSH tunnel forwarding, it
125 * uses some preprocessor definitions for various platform-specific
126 * settings.
127 *
128 * SSH_TUN_LINUX Use the (newer) Linux tun/tap device
Darren Tucker3eb48342006-06-23 21:05:12 +1000129 * SSH_TUN_FREEBSD Use the FreeBSD tun/tap device
Damien Miller598bbc22005-12-31 16:33:36 +1100130 * SSH_TUN_COMPAT_AF Translate the OpenBSD address family
131 * SSH_TUN_PREPEND_AF Prepend/remove the address family
132 */
133
134/*
135 * System-specific tunnel open function
136 */
137
138#if defined(SSH_TUN_LINUX)
139#include <linux/if_tun.h>
140
141int
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000142sys_tun_open(int tun, int mode, char **ifname)
Damien Miller598bbc22005-12-31 16:33:36 +1100143{
144 struct ifreq ifr;
145 int fd = -1;
146 const char *name = NULL;
147
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000148 if (ifname != NULL)
149 *ifname = NULL;
150
Damien Miller598bbc22005-12-31 16:33:36 +1100151 if ((fd = open("/dev/net/tun", O_RDWR)) == -1) {
152 debug("%s: failed to open tunnel control interface: %s",
153 __func__, strerror(errno));
154 return (-1);
155 }
156
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000157 bzero(&ifr, sizeof(ifr));
Damien Miller598bbc22005-12-31 16:33:36 +1100158
159 if (mode == SSH_TUNMODE_ETHERNET) {
160 ifr.ifr_flags = IFF_TAP;
161 name = "tap%d";
162 } else {
163 ifr.ifr_flags = IFF_TUN;
164 name = "tun%d";
165 }
166 ifr.ifr_flags |= IFF_NO_PI;
167
168 if (tun != SSH_TUNID_ANY) {
169 if (tun > SSH_TUNID_MAX) {
170 debug("%s: invalid tunnel id %x: %s", __func__,
171 tun, strerror(errno));
172 goto failed;
173 }
174 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), name, tun);
175 }
176
177 if (ioctl(fd, TUNSETIFF, &ifr) == -1) {
178 debug("%s: failed to configure tunnel (mode %d): %s", __func__,
179 mode, strerror(errno));
180 goto failed;
181 }
182
183 if (tun == SSH_TUNID_ANY)
184 debug("%s: tunnel mode %d fd %d", __func__, mode, fd);
185 else
186 debug("%s: %s mode %d fd %d", __func__, ifr.ifr_name, mode, fd);
187
Damien Millercfb1d9b2018-04-13 13:38:06 +1000188 if (ifname != NULL && (*ifname = strdup(ifr.ifr_name)) == NULL)
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000189 goto failed;
190
Damien Miller598bbc22005-12-31 16:33:36 +1100191 return (fd);
192
193 failed:
194 close(fd);
195 return (-1);
196}
197#endif /* SSH_TUN_LINUX */
198
Damien Miller2dcddbf2006-01-01 19:47:05 +1100199#ifdef SSH_TUN_FREEBSD
200#include <sys/socket.h>
201#include <net/if.h>
Darren Tucker3eb48342006-06-23 21:05:12 +1000202
203#ifdef HAVE_NET_IF_TUN_H
Damien Miller2dcddbf2006-01-01 19:47:05 +1100204#include <net/if_tun.h>
Darren Tucker3eb48342006-06-23 21:05:12 +1000205#endif
Damien Miller2dcddbf2006-01-01 19:47:05 +1100206
207int
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000208sys_tun_open(int tun, int mode, char **ifname)
Damien Miller2dcddbf2006-01-01 19:47:05 +1100209{
210 struct ifreq ifr;
211 char name[100];
212 int fd = -1, sock, flag;
213 const char *tunbase = "tun";
214
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000215 if (ifname != NULL)
216 *ifname = NULL;
217
Damien Miller2dcddbf2006-01-01 19:47:05 +1100218 if (mode == SSH_TUNMODE_ETHERNET) {
219#ifdef SSH_TUN_NO_L2
220 debug("%s: no layer 2 tunnelling support", __func__);
221 return (-1);
222#else
223 tunbase = "tap";
224#endif
225 }
226
227 /* Open the tunnel device */
228 if (tun <= SSH_TUNID_MAX) {
229 snprintf(name, sizeof(name), "/dev/%s%d", tunbase, tun);
230 fd = open(name, O_RDWR);
231 } else if (tun == SSH_TUNID_ANY) {
232 for (tun = 100; tun >= 0; tun--) {
233 snprintf(name, sizeof(name), "/dev/%s%d",
234 tunbase, tun);
235 if ((fd = open(name, O_RDWR)) >= 0)
236 break;
237 }
238 } else {
239 debug("%s: invalid tunnel %u\n", __func__, tun);
240 return (-1);
241 }
242
243 if (fd < 0) {
244 debug("%s: %s open failed: %s", __func__, name,
245 strerror(errno));
246 return (-1);
247 }
248
249 /* Turn on tunnel headers */
250 flag = 1;
251#if defined(TUNSIFHEAD) && !defined(SSH_TUN_PREPEND_AF)
252 if (mode != SSH_TUNMODE_ETHERNET &&
253 ioctl(fd, TUNSIFHEAD, &flag) == -1) {
254 debug("%s: ioctl(%d, TUNSIFHEAD, 1): %s", __func__, fd,
255 strerror(errno));
256 close(fd);
257 }
258#endif
259
260 debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
261
262 /* Set the tunnel device operation mode */
263 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", tunbase, tun);
264 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
265 goto failed;
266
267 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
268 goto failed;
Damien Miller2c4b13a2010-08-10 12:47:40 +1000269 if ((ifr.ifr_flags & IFF_UP) == 0) {
270 ifr.ifr_flags |= IFF_UP;
271 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
272 goto failed;
273 }
Damien Miller2dcddbf2006-01-01 19:47:05 +1100274
Damien Millercfb1d9b2018-04-13 13:38:06 +1000275 if (ifname != NULL && (*ifname = strdup(ifr.ifr_name)) == NULL)
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000276 goto failed;
277
Damien Miller2dcddbf2006-01-01 19:47:05 +1100278 close(sock);
279 return (fd);
280
281 failed:
282 if (fd >= 0)
283 close(fd);
284 if (sock >= 0)
285 close(sock);
286 debug("%s: failed to set %s mode %d: %s", __func__, name,
287 mode, strerror(errno));
288 return (-1);
289}
290#endif /* SSH_TUN_FREEBSD */
291
Damien Miller598bbc22005-12-31 16:33:36 +1100292/*
293 * System-specific channel filters
294 */
295
296#if defined(SSH_TUN_FILTER)
Damien Millerc78e6ee2017-07-21 14:38:16 +1000297/*
298 * The tunnel forwarding protocol prepends the address family of forwarded
299 * IP packets using OpenBSD's numbers.
300 */
Damien Miller598bbc22005-12-31 16:33:36 +1100301#define OPENBSD_AF_INET 2
302#define OPENBSD_AF_INET6 24
303
304int
Damien Miller871f1e42017-09-12 18:01:35 +1000305sys_tun_infilter(struct ssh *ssh, struct Channel *c, char *buf, int _len)
Damien Miller598bbc22005-12-31 16:33:36 +1100306{
Damien Millerc78e6ee2017-07-21 14:38:16 +1000307 int r;
308 size_t len;
309 char *ptr = buf;
Damien Miller598bbc22005-12-31 16:33:36 +1100310#if defined(SSH_TUN_PREPEND_AF)
311 char rbuf[CHAN_RBUF];
Damien Millerc78e6ee2017-07-21 14:38:16 +1000312 struct ip iph;
Damien Miller598bbc22005-12-31 16:33:36 +1100313#endif
Damien Millerc78e6ee2017-07-21 14:38:16 +1000314#if defined(SSH_TUN_PREPEND_AF) || defined(SSH_TUN_COMPAT_AF)
315 u_int32_t af;
316#endif
317
318 /* XXX update channel input filter API to use unsigned length */
319 if (_len < 0)
320 return -1;
321 len = _len;
Damien Miller598bbc22005-12-31 16:33:36 +1100322
323#if defined(SSH_TUN_PREPEND_AF)
Damien Millerc78e6ee2017-07-21 14:38:16 +1000324 if (len <= sizeof(iph) || len > sizeof(rbuf) - 4)
325 return -1;
326 /* Determine address family from packet IP header. */
327 memcpy(&iph, buf, sizeof(iph));
328 af = iph.ip_v == 6 ? OPENBSD_AF_INET6 : OPENBSD_AF_INET;
329 /* Prepend address family to packet using OpenBSD constants */
330 memcpy(rbuf + 4, buf, len);
331 len += 4;
332 POKE_U32(rbuf, af);
333 ptr = rbuf;
334#elif defined(SSH_TUN_COMPAT_AF)
335 /* Convert existing address family header to OpenBSD value */
336 if (len <= 4)
337 return -1;
338 af = PEEK_U32(buf);
339 /* Put it back */
340 POKE_U32(buf, af == AF_INET6 ? OPENBSD_AF_INET6 : OPENBSD_AF_INET);
Damien Miller598bbc22005-12-31 16:33:36 +1100341#endif
Damien Miller2dcddbf2006-01-01 19:47:05 +1100342
Damien Miller871f1e42017-09-12 18:01:35 +1000343 if ((r = sshbuf_put_string(c->input, ptr, len)) != 0)
Damien Miller0cdc5a32015-01-15 02:35:33 +1100344 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller598bbc22005-12-31 16:33:36 +1100345 return (0);
346}
347
348u_char *
Damien Miller871f1e42017-09-12 18:01:35 +1000349sys_tun_outfilter(struct ssh *ssh, struct Channel *c,
350 u_char **data, size_t *dlen)
Damien Miller598bbc22005-12-31 16:33:36 +1100351{
352 u_char *buf;
Damien Millerc78e6ee2017-07-21 14:38:16 +1000353 u_int32_t af;
Damien Miller0cdc5a32015-01-15 02:35:33 +1100354 int r;
Damien Miller598bbc22005-12-31 16:33:36 +1100355
Damien Millerbc42cc62015-01-15 03:08:29 +1100356 /* XXX new API is incompatible with this signature. */
Damien Miller871f1e42017-09-12 18:01:35 +1000357 if ((r = sshbuf_get_string(c->output, data, dlen)) != 0)
Damien Miller0cdc5a32015-01-15 02:35:33 +1100358 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerc78e6ee2017-07-21 14:38:16 +1000359 if (*dlen < sizeof(af))
Damien Miller598bbc22005-12-31 16:33:36 +1100360 return (NULL);
361 buf = *data;
362
363#if defined(SSH_TUN_PREPEND_AF)
Damien Millerc78e6ee2017-07-21 14:38:16 +1000364 /* skip address family */
365 *dlen -= sizeof(af);
366 buf = *data + sizeof(af);
Damien Miller598bbc22005-12-31 16:33:36 +1100367#elif defined(SSH_TUN_COMPAT_AF)
Damien Millerc78e6ee2017-07-21 14:38:16 +1000368 /* translate address family */
369 af = (PEEK_U32(buf) == OPENBSD_AF_INET6) ? AF_INET6 : AF_INET;
370 POKE_U32(buf, af);
Damien Miller598bbc22005-12-31 16:33:36 +1100371#endif
Damien Miller598bbc22005-12-31 16:33:36 +1100372 return (buf);
373}
374#endif /* SSH_TUN_FILTER */