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