… | |
… | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
15 | GNU General Public License for more details. |
16 | |
16 | |
17 | 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 |
18 | along with gvpe; if not, write to the Free Software |
18 | along with gvpe; if not, write to the Free Software |
19 | 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 |
20 | */ |
20 | */ |
21 | |
21 | |
22 | #include "config.h" |
22 | #include "config.h" |
23 | |
23 | |
24 | #include <list> |
24 | #include <list> |
… | |
… | |
58 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
58 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
59 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
59 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
60 | |
60 | |
61 | char *env; |
61 | char *env; |
62 | asprintf (&env, "CONFBASE=%s", confbase); putenv (env); |
62 | asprintf (&env, "CONFBASE=%s", confbase); putenv (env); |
63 | asprintf (&env, "NODENAME=%s", THISNODE->nodename); putenv (env); |
|
|
64 | asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env); |
|
|
65 | asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); |
63 | asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); |
66 | asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env); |
64 | asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env); |
67 | asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env); |
65 | asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env); |
68 | asprintf (&env, "MTU=%d", mtu); putenv (env); |
66 | asprintf (&env, "MTU=%d", mtu); putenv (env); |
69 | asprintf (&env, "MAC=%02x:%02x:%02x:%02x:%02x:%02x", |
67 | asprintf (&env, "NODES=%d", conns.size ()); putenv (env); |
70 | 0xfe, 0xfd, 0x80, 0x00, THISNODE->id >> 8, |
68 | asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env); |
71 | THISNODE->id & 0xff); putenv (env); |
|
|
72 | |
69 | |
73 | // TODO: info for other nodes, maybe? |
70 | conns [THISNODE->id - 1]->script_init_env (""); |
|
|
71 | |
|
|
72 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
|
|
73 | { |
|
|
74 | char ext[16]; |
|
|
75 | snprintf (ext, 16, "_%d", (*c)->conf->id); |
|
|
76 | (*c)->script_init_env (ext); |
|
|
77 | } |
74 | } |
78 | } |
75 | |
79 | |
76 | const char *vpn::script_if_init () |
80 | const char *vpn::script_if_init () |
77 | { |
81 | { |
78 | script_init_env (); |
82 | script_init_env (); |
… | |
… | |
104 | |
108 | |
105 | if (ipv4_fd < 0) |
109 | if (ipv4_fd < 0) |
106 | return -1; |
110 | return -1; |
107 | |
111 | |
108 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
112 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
|
|
113 | fcntl (ipv4_fd, F_SETFD, FD_CLOEXEC); |
109 | |
114 | |
110 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
115 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
111 | // this I really consider a linux bug. I am neither connected |
116 | // this I really consider a linux bug. I am neither connected |
112 | // nor do I fragment myself. Linux still sets DF and doesn't |
117 | // nor do I fragment myself. Linux still sets DF and doesn't |
113 | // fragment for me sometimes. |
118 | // fragment for me sometimes. |
… | |
… | |
136 | |
141 | |
137 | if (udpv4_fd < 0) |
142 | if (udpv4_fd < 0) |
138 | return -1; |
143 | return -1; |
139 | |
144 | |
140 | fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
145 | fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
146 | fcntl (udpv4_fd, F_SETFD, FD_CLOEXEC); |
141 | |
147 | |
142 | // standard daemon practise... |
148 | // standard daemon practise... |
143 | { |
149 | { |
144 | int oval = 1; |
150 | int oval = 1; |
145 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
151 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
… | |
… | |
175 | |
181 | |
176 | if (icmpv4_fd < 0) |
182 | if (icmpv4_fd < 0) |
177 | return -1; |
183 | return -1; |
178 | |
184 | |
179 | fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
185 | fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
186 | fcntl (icmpv4_fd, F_SETFD, FD_CLOEXEC); |
180 | |
187 | |
181 | #ifdef ICMP_FILTER |
188 | #ifdef ICMP_FILTER |
182 | { |
189 | { |
183 | icmp_filter oval; |
190 | icmp_filter oval; |
184 | oval.data = 0xffffffff; |
191 | oval.data = 0xffffffff; |
… | |
… | |
193 | // this I really consider a linux bug. I am neither connected |
200 | // this I really consider a linux bug. I am neither connected |
194 | // nor do I fragment myself. Linux still sets DF and doesn't |
201 | // nor do I fragment myself. Linux still sets DF and doesn't |
195 | // fragment for me sometimes. |
202 | // fragment for me sometimes. |
196 | { |
203 | { |
197 | int oval = IP_PMTUDISC_DONT; |
204 | int oval = IP_PMTUDISC_DONT; |
198 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
205 | setsockopt (icmpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
199 | } |
206 | } |
200 | #endif |
207 | #endif |
201 | |
208 | |
202 | sockinfo si (THISNODE, PROT_ICMPv4); |
209 | sockinfo si (THISNODE, PROT_ICMPv4); |
203 | |
210 | |
… | |
… | |
220 | |
227 | |
221 | if (tcpv4_fd < 0) |
228 | if (tcpv4_fd < 0) |
222 | return -1; |
229 | return -1; |
223 | |
230 | |
224 | fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
231 | fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
232 | fcntl (tcpv4_fd, F_SETFD, FD_CLOEXEC); |
225 | |
233 | |
226 | // standard daemon practise... |
234 | // standard daemon practise... |
227 | { |
235 | { |
228 | int oval = 1; |
236 | int oval = 1; |
229 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
237 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
… | |
… | |
255 | dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
263 | dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
256 | |
264 | |
257 | if (dnsv4_fd < 0) |
265 | if (dnsv4_fd < 0) |
258 | return -1; |
266 | return -1; |
259 | |
267 | |
|
|
268 | fcntl (dnsv4_fd, F_SETFL, O_NONBLOCK); |
|
|
269 | fcntl (dnsv4_fd, F_SETFD, FD_CLOEXEC); |
|
|
270 | |
260 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
271 | # if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
261 | // this I really consider a linux bug. I am neither connected |
272 | // this I really consider a linux bug. I am neither connected |
262 | // nor do I fragment myself. Linux still sets DF and doesn't |
273 | // nor do I fragment myself. Linux still sets DF and doesn't |
263 | // fragment for me sometimes. |
274 | // fragment for me sometimes. |
264 | { |
275 | { |
265 | int oval = IP_PMTUDISC_DONT; |
276 | int oval = IP_PMTUDISC_DONT; |
266 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
277 | setsockopt (dnsv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
267 | } |
278 | } |
268 | #endif |
279 | # endif |
269 | |
280 | |
270 | // standard daemon practise... |
281 | // standard daemon practise... |
271 | { |
282 | { |
272 | int oval = 1; |
283 | int oval = 1; |
273 | setsockopt (dnsv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
284 | setsockopt (dnsv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
… | |
… | |
285 | |
296 | |
286 | dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); |
297 | dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); |
287 | } |
298 | } |
288 | #endif |
299 | #endif |
289 | |
300 | |
|
|
301 | ///////////////////////////////////////////////////////////////////////////// |
|
|
302 | |
|
|
303 | reconnect_all (); |
|
|
304 | |
|
|
305 | ///////////////////////////////////////////////////////////////////////////// |
|
|
306 | |
290 | tap = new tap_device (); |
307 | tap = new tap_device (); |
291 | if (!tap) //D this, of course, never catches |
308 | if (!tap) //D this, of course, never catches |
292 | { |
309 | { |
293 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
310 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
294 | exit (EXIT_FAILURE); |
311 | exit (EXIT_FAILURE); |
295 | } |
312 | } |
296 | |
313 | |
|
|
314 | fcntl (tap->fd, F_SETFD, FD_CLOEXEC); |
|
|
315 | |
297 | if (tap->if_up () && |
316 | if (tap->if_up () && |
298 | !run_script (run_script_cb (this, &vpn::script_if_init), true)) |
317 | !run_script (run_script_cb (this, &vpn::script_if_init), true)) |
299 | { |
318 | { |
300 | slog (L_ERR, _("interface initialization command '%s' failed, exiting."), |
319 | slog (L_ERR, _("interface initialization command '%s' failed, exiting."), |
301 | tap->if_up ()); |
320 | tap->if_up ()); |
… | |
… | |
307 | slog (L_ERR, _("if-up command execution failed, exiting.")); |
326 | slog (L_ERR, _("if-up command execution failed, exiting.")); |
308 | exit (EXIT_FAILURE); |
327 | exit (EXIT_FAILURE); |
309 | } |
328 | } |
310 | |
329 | |
311 | tap_ev_watcher.start (tap->fd, EVENT_READ); |
330 | tap_ev_watcher.start (tap->fd, EVENT_READ); |
312 | |
|
|
313 | reconnect_all (); |
|
|
314 | |
331 | |
315 | return 0; |
332 | return 0; |
316 | } |
333 | } |
317 | |
334 | |
318 | bool |
335 | bool |