1 | /* |
1 | /* |
2 | device-tincd.C -- include one of the tincd low level implementations. |
2 | device-tincd.C -- include one of the tincd low level implementations. |
|
|
3 | Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> |
3 | |
4 | |
|
|
5 | This file is part of GVPE. |
|
|
6 | |
4 | This program is free software; you can redistribute it and/or modify |
7 | GVPE is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
8 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
9 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
10 | (at your option) any later version. |
8 | |
11 | |
9 | This program is distributed in the hope that it will be useful, |
12 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
15 | GNU General Public License for more details. |
13 | |
16 | |
14 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software |
18 | along with gvpe; if not, write to the Free Software |
16 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | */ |
20 | */ |
18 | |
21 | |
19 | #include <cstdio> |
22 | #include <cstdio> |
20 | #include <cstring> |
23 | #include <cstring> |
21 | #include <cerrno> |
24 | #include <cerrno> |
… | |
… | |
58 | #define ifdebug(subsys) if (0) |
61 | #define ifdebug(subsys) if (0) |
59 | |
62 | |
60 | #define cp() |
63 | #define cp() |
61 | #define lookup_config(config_tree,key) (key) |
64 | #define lookup_config(config_tree,key) (key) |
62 | |
65 | |
63 | #define MTU MAXSIZE |
66 | #define MTU MAX_MTU |
64 | |
67 | |
65 | // BIGGEST hack of 'em all |
68 | // BIGGEST hack of 'em all |
66 | // will be casted to data_packet, due to structural similarity |
69 | // will be casted to data_packet, due to structural similarity |
67 | struct vpn_packet_t : net_packet { |
70 | struct vpn_packet_t : net_packet |
|
|
71 | { |
68 | u8 data[MAXSIZE]; |
72 | u8 data[MAXSIZE]; |
69 | }; |
73 | }; |
70 | |
|
|
71 | static tap_device *self; |
|
|
72 | |
74 | |
73 | static bool overwrite_mac; |
75 | static bool overwrite_mac; |
74 | |
76 | |
75 | static bool |
77 | static bool |
76 | get_config_string(const char *key, char **res) |
78 | get_config_string(const char *key, char **res) |
77 | { |
79 | { |
78 | if (!strcmp (key, "Interface")) |
80 | if (!strcmp (key, "Interface")) |
79 | *res = conf.ifname; |
81 | *res = conf.ifname; |
80 | else if (!strcmp (key, "Device")) |
82 | else if (!strcmp (key, "Device")) |
81 | *res = 0; |
83 | *res = 0; |
|
|
84 | else if (!strcmp (key, "DeviceType")) |
|
|
85 | *res = "tap"; |
82 | else |
86 | else |
83 | { |
87 | { |
84 | slog (L_ERR, _("tincd layer asking for unknown config '%s'"), key); |
88 | slog (L_ERR, _("tincd layer asking for unknown config '%s'"), key); |
85 | *res = 0; |
89 | *res = 0; |
86 | } |
90 | } |
… | |
… | |
90 | |
94 | |
91 | #define netname conf.ifname |
95 | #define netname conf.ifname |
92 | |
96 | |
93 | #if IF_linux |
97 | #if IF_linux |
94 | # include "tincd/linux/device.c" |
98 | # include "tincd/linux/device.c" |
|
|
99 | const char * tap_device::if_up () { return "/sbin/ifconfig $IFNAME hw ether $MAC mtu $MTU"; } |
|
|
100 | |
|
|
101 | #elif IF_bsd |
|
|
102 | # include "tincd/bsd/device.c" |
|
|
103 | const char * tap_device::if_up () { return "/sbin/ifconfig $IFNAME ether $MAC mtu $MTU"; } |
|
|
104 | |
95 | #elif IF_freebsd |
105 | #elif IF_freebsd |
96 | # include "tincd/freebsd/device.c" |
106 | # include "tincd/freebsd/device.c" |
|
|
107 | const char * tap_device::if_up () { return "/sbin/ifconfig $IFNAME ether $MAC mtu $MTU"; } |
|
|
108 | |
97 | #elif IF_netbsd |
109 | #elif IF_netbsd |
|
|
110 | # define IF_istun 1 |
98 | # include "tincd/netbsd/device.c" |
111 | # include "tincd/netbsd/device.c" |
|
|
112 | const char * tap_device::if_up () { return "/sbin/ifconfig $IFNAME mtu $MTU"; } |
|
|
113 | |
|
|
114 | #elif IF_openbsd |
|
|
115 | # define IF_istun 1 |
|
|
116 | # include "tincd/openbsd/device.c" |
|
|
117 | const char * tap_device::if_up () { return "/sbin/ifconfig $IFNAME mtu $MTU"; } |
|
|
118 | |
99 | #elif IF_solaris |
119 | #elif IF_solaris |
|
|
120 | # define IF_istun 1 |
100 | # include "tincd/solaris/device.c" |
121 | # include "tincd/solaris/device.c" |
|
|
122 | const char * tap_device::if_up () { return ""; } |
|
|
123 | |
101 | #elif IF_cygwin |
124 | #elif IF_cygwin |
102 | # include "tincd/cygwin/device.c" |
125 | # include "tincd/cygwin/device.c" |
|
|
126 | const char * tap_device::if_up () { return ""; } |
|
|
127 | |
103 | #elif IF_mingw |
128 | #elif IF_mingw |
104 | # include "tincd/mingw/device.c" |
129 | # include "tincd/mingw/device.c" |
|
|
130 | const char * tap_device::if_up () { return ""; } |
|
|
131 | |
105 | #elif IF_darwin |
132 | #elif IF_darwin |
|
|
133 | # define IF_istun 1 |
106 | # include "tincd/darwin/device.c" |
134 | # include "tincd/darwin/device.c" |
|
|
135 | const char * tap_device::if_up () { return "/sbin/ifconfig $IFNAME ether $MAC mtu $MTU"; } |
|
|
136 | |
107 | #elif IF_raw_socket |
137 | #elif IF_raw_socket |
108 | # include "tincd/raw_socket/device.c" |
138 | # include "tincd/raw_socket/device.c" |
|
|
139 | const char * tap_device::if_up () { return "/sbin/ifconfig $IFNAME ether $MAC mtu $MTU"; } |
|
|
140 | |
|
|
141 | #elif IF_uml_socket |
|
|
142 | # include "tincd/uml_socket/device.c" |
|
|
143 | const char * tap_device::if_up () { return 0; } |
|
|
144 | |
109 | #else |
145 | #else |
110 | # error No interface implementation for your IFTYPE/IFSUBTYPE combination. |
146 | # error No interface implementation for your IFTYPE/IFSUBTYPE combination. |
111 | #endif |
147 | #endif |
112 | |
148 | |
|
|
149 | #if IF_istun |
|
|
150 | # include "ether_emu.C" |
|
|
151 | #endif |
|
|
152 | |
113 | const char * |
153 | const char * |
114 | tap_device::info () |
154 | tap_device::info () |
115 | { |
155 | { |
116 | return _("tincd compatibility layer"); |
156 | return _("tincd compatibility layer"); |
117 | } |
157 | } |
118 | |
158 | |
119 | tap_device::tap_device () |
159 | tap_device::tap_device () |
120 | { |
160 | { |
121 | self = this; |
161 | device = "(null)"; |
122 | |
162 | |
123 | if (setup_device ()) |
163 | bool ok = setup_device (); |
|
|
164 | |
|
|
165 | if (device_info) |
|
|
166 | device = device_info; |
|
|
167 | |
|
|
168 | if (ok) |
124 | { |
169 | { |
125 | //slog (L_DEBUG, _("%s is a %s"), device, info ()); |
170 | slog (L_DEBUG, _("interface %s on %s initialized"), info (), device); |
126 | fd = device_fd; |
171 | fd = device_fd; |
127 | strcpy (ifrname, iface); |
172 | strcpy (ifrname, iface); |
128 | } |
173 | } |
129 | else |
174 | else |
130 | { |
175 | { |
131 | slog (L_ERR, _("error while configuring tincd device (%s/%s)"), device, info ()); |
176 | slog (L_ERR, _("error while configuring tincd device %s on %s"), info (), device); |
132 | exit (1); |
177 | exit (EXIT_FAILURE); |
133 | } |
178 | } |
134 | } |
179 | } |
135 | |
180 | |
136 | tap_device::~tap_device () |
181 | tap_device::~tap_device () |
137 | { |
182 | { |
… | |
… | |
144 | tap_packet *pkt = new tap_packet; |
189 | tap_packet *pkt = new tap_packet; |
145 | |
190 | |
146 | if (!read_packet (reinterpret_cast<vpn_packet_t *>(pkt))) |
191 | if (!read_packet (reinterpret_cast<vpn_packet_t *>(pkt))) |
147 | { |
192 | { |
148 | delete pkt; |
193 | delete pkt; |
149 | slog (L_ERR, _("can't read from to %s %s: %s"), info (), DEFAULT_DEVICE, |
194 | slog (L_ERR, _("can't read from to %s %s: %s"), info (), device, |
150 | strerror (errno)); |
195 | strerror (errno)); |
151 | return 0; |
196 | return 0; |
152 | } |
197 | } |
153 | |
198 | |
|
|
199 | #if IF_istun |
|
|
200 | // assume ipv4 |
|
|
201 | (*pkt)[12] = 0x08; |
|
|
202 | (*pkt)[13] = 0x00; |
|
|
203 | |
|
|
204 | if (!ether_emu.tun_to_tap (pkt)) |
|
|
205 | { |
|
|
206 | delete pkt; |
|
|
207 | return 0; |
|
|
208 | } |
|
|
209 | #endif |
|
|
210 | |
154 | return pkt; |
211 | return pkt; |
155 | } |
212 | } |
156 | |
213 | |
157 | void |
214 | void |
158 | tap_device::send (tap_packet *pkt) |
215 | tap_device::send (tap_packet *pkt) |
159 | { |
216 | { |
|
|
217 | if ( |
|
|
218 | #if IF_istun |
|
|
219 | ether_emu.tap_to_tun (pkt) && |
|
|
220 | #endif |
160 | if (!write_packet (reinterpret_cast<vpn_packet_t *>(pkt))) |
221 | !write_packet (reinterpret_cast<vpn_packet_t *>(pkt))) |
161 | slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
222 | slog (L_ERR, _("can't write to %s %s: %s"), info (), device, |
162 | strerror (errno)); |
223 | strerror (errno)); |
163 | } |
224 | } |
164 | |
225 | |
165 | |
226 | |