1 |
pcg |
1.1 |
/* |
2 |
|
|
device-tincd.C -- include one of the tincd low level implementations. |
3 |
pcg |
1.9 |
Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com> |
4 |
pcg |
1.1 |
|
5 |
|
|
This program is free software; you can redistribute it and/or modify |
6 |
|
|
it under the terms of the GNU General Public License as published by |
7 |
|
|
the Free Software Foundation; either version 2 of the License, or |
8 |
|
|
(at your option) any later version. |
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, |
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
GNU General Public License for more details. |
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License |
16 |
|
|
along with this program; if not, write to the Free Software |
17 |
|
|
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 |
|
|
*/ |
19 |
|
|
|
20 |
|
|
#include <cstdio> |
21 |
|
|
#include <cstring> |
22 |
|
|
#include <cerrno> |
23 |
|
|
|
24 |
|
|
#include <sys/types.h> |
25 |
|
|
#include <sys/stat.h> |
26 |
|
|
#include <sys/ioctl.h> |
27 |
|
|
#include <unistd.h> |
28 |
|
|
#include <syslog.h> |
29 |
|
|
#include <fcntl.h> |
30 |
|
|
|
31 |
|
|
#include "conf.h" |
32 |
|
|
|
33 |
pcg |
1.3 |
// following headers used by cygwin (maybe others) |
34 |
|
|
#include "netcompat.h" |
35 |
|
|
#include <signal.h> |
36 |
|
|
|
37 |
|
|
#define xstrdup(strd) strdup(str) |
38 |
|
|
|
39 |
pcg |
1.2 |
/* make the tincd sources feel comfortable in our environment. */ |
40 |
|
|
/* this was reasonably easy to do. */ |
41 |
pcg |
1.1 |
#define routing_mode 1 |
42 |
|
|
#define RMODE_ROUTER 0 |
43 |
|
|
|
44 |
pcg |
1.2 |
#define LOG_TO_L(level) \ |
45 |
pcg |
1.1 |
(level) == LOG_ERR ? L_ERR \ |
46 |
|
|
: (level) == LOG_DEBUG ? L_DEBUG \ |
47 |
|
|
: (level) == LOG_WARNING ? L_WARN \ |
48 |
|
|
: (level) == LOG_INFO ? L_INFO \ |
49 |
pcg |
1.2 |
: L_NOTICE |
50 |
|
|
|
51 |
|
|
#if __STDC_VERSION__ > 199900 |
52 |
|
|
# define logger(level, ...) slog (LOG_TO_L(level), __VA_ARGS__) |
53 |
|
|
#elif __GNUC__ |
54 |
|
|
# define logger(level, args...) slog (LOG_TO_L(level), ## args) |
55 |
|
|
#else |
56 |
|
|
# error either need ISO-C 99 compliant compiler or gcc. |
57 |
|
|
#endif |
58 |
pcg |
1.1 |
|
59 |
|
|
#define ifdebug(subsys) if (0) |
60 |
|
|
|
61 |
|
|
#define cp() |
62 |
|
|
#define lookup_config(config_tree,key) (key) |
63 |
|
|
|
64 |
|
|
#define MTU MAXSIZE |
65 |
|
|
|
66 |
|
|
// BIGGEST hack of 'em all |
67 |
|
|
// will be casted to data_packet, due to structural similarity |
68 |
|
|
struct vpn_packet_t : net_packet { |
69 |
|
|
u8 data[MAXSIZE]; |
70 |
|
|
}; |
71 |
|
|
|
72 |
|
|
static bool overwrite_mac; |
73 |
|
|
|
74 |
|
|
static bool |
75 |
|
|
get_config_string(const char *key, char **res) |
76 |
|
|
{ |
77 |
|
|
if (!strcmp (key, "Interface")) |
78 |
|
|
*res = conf.ifname; |
79 |
|
|
else if (!strcmp (key, "Device")) |
80 |
|
|
*res = 0; |
81 |
|
|
else |
82 |
|
|
{ |
83 |
|
|
slog (L_ERR, _("tincd layer asking for unknown config '%s'"), key); |
84 |
|
|
*res = 0; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
return *res; |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
#define netname conf.ifname |
91 |
|
|
|
92 |
|
|
#if IF_linux |
93 |
|
|
# include "tincd/linux/device.c" |
94 |
pcg |
1.6 |
|
95 |
pcg |
1.1 |
#elif IF_freebsd |
96 |
|
|
# include "tincd/freebsd/device.c" |
97 |
pcg |
1.6 |
|
98 |
pcg |
1.1 |
#elif IF_netbsd |
99 |
pcg |
1.5 |
#define IF_istun 1 |
100 |
pcg |
1.1 |
# include "tincd/netbsd/device.c" |
101 |
pcg |
1.6 |
|
102 |
|
|
#elif IF_openbsd |
103 |
|
|
#define IF_istun 1 |
104 |
|
|
# include "tincd/openbsd/device.c" |
105 |
|
|
|
106 |
pcg |
1.1 |
#elif IF_solaris |
107 |
|
|
# include "tincd/solaris/device.c" |
108 |
pcg |
1.6 |
|
109 |
pcg |
1.1 |
#elif IF_cygwin |
110 |
|
|
# include "tincd/cygwin/device.c" |
111 |
pcg |
1.6 |
|
112 |
pcg |
1.1 |
#elif IF_mingw |
113 |
|
|
# include "tincd/mingw/device.c" |
114 |
pcg |
1.6 |
|
115 |
pcg |
1.1 |
#elif IF_darwin |
116 |
pcg |
1.5 |
#define IF_istun 1 |
117 |
pcg |
1.1 |
# include "tincd/darwin/device.c" |
118 |
pcg |
1.6 |
|
119 |
pcg |
1.1 |
#elif IF_raw_socket |
120 |
pcg |
1.5 |
#define IF_istun 1 |
121 |
pcg |
1.1 |
# include "tincd/raw_socket/device.c" |
122 |
pcg |
1.6 |
|
123 |
pcg |
1.1 |
#else |
124 |
|
|
# error No interface implementation for your IFTYPE/IFSUBTYPE combination. |
125 |
|
|
#endif |
126 |
|
|
|
127 |
pcg |
1.5 |
#if IF_istun |
128 |
|
|
# include "ether_emu.C" |
129 |
|
|
#endif |
130 |
|
|
|
131 |
pcg |
1.1 |
const char * |
132 |
|
|
tap_device::info () |
133 |
|
|
{ |
134 |
|
|
return _("tincd compatibility layer"); |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
tap_device::tap_device () |
138 |
|
|
{ |
139 |
pcg |
1.8 |
device = "(null)"; |
140 |
|
|
|
141 |
pcg |
1.1 |
if (setup_device ()) |
142 |
|
|
{ |
143 |
pcg |
1.8 |
slog (L_DEBUG, _("interface %s on %s initialized"), info (), device); |
144 |
pcg |
1.1 |
fd = device_fd; |
145 |
|
|
strcpy (ifrname, iface); |
146 |
|
|
} |
147 |
|
|
else |
148 |
|
|
{ |
149 |
pcg |
1.8 |
slog (L_ERR, _("error while configuring tincd device %s on %s"), info (), device); |
150 |
pcg |
1.1 |
exit (1); |
151 |
|
|
} |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
tap_device::~tap_device () |
155 |
|
|
{ |
156 |
|
|
close_device (); |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
tap_packet * |
160 |
|
|
tap_device::recv () |
161 |
|
|
{ |
162 |
|
|
tap_packet *pkt = new tap_packet; |
163 |
|
|
|
164 |
|
|
if (!read_packet (reinterpret_cast<vpn_packet_t *>(pkt))) |
165 |
|
|
{ |
166 |
|
|
delete pkt; |
167 |
|
|
slog (L_ERR, _("can't read from to %s %s: %s"), info (), DEFAULT_DEVICE, |
168 |
|
|
strerror (errno)); |
169 |
|
|
return 0; |
170 |
|
|
} |
171 |
|
|
|
172 |
pcg |
1.5 |
#if IF_istun |
173 |
|
|
// assume ipv4 |
174 |
|
|
(*pkt)[12] = 0x08; |
175 |
|
|
(*pkt)[13] = 0x00; |
176 |
|
|
|
177 |
|
|
if (!ether_emu.tun_to_tap (pkt)) |
178 |
|
|
{ |
179 |
|
|
delete pkt; |
180 |
|
|
return 0; |
181 |
|
|
} |
182 |
|
|
#endif |
183 |
|
|
|
184 |
pcg |
1.1 |
return pkt; |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
void |
188 |
|
|
tap_device::send (tap_packet *pkt) |
189 |
|
|
{ |
190 |
pcg |
1.5 |
if ( |
191 |
|
|
#if IF_istun |
192 |
|
|
ether_emu.tap_to_tun (pkt) && |
193 |
|
|
#endif |
194 |
|
|
!write_packet (reinterpret_cast<vpn_packet_t *>(pkt))) |
195 |
pcg |
1.1 |
slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
196 |
|
|
strerror (errno)); |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
|