… | |
… | |
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> |
… | |
… | |
46 | |
46 | |
47 | vpn network; // THE vpn (bad design...) |
47 | vpn network; // THE vpn (bad design...) |
48 | |
48 | |
49 | ///////////////////////////////////////////////////////////////////////////// |
49 | ///////////////////////////////////////////////////////////////////////////// |
50 | |
50 | |
51 | const char *vpn::script_if_up () |
51 | void |
|
|
52 | vpn::script_init_env () |
52 | { |
53 | { |
53 | // the tunnel device mtu should be the physical mtu - overhead |
54 | // the tunnel device mtu should be the physical mtu - overhead |
54 | // the tricky part is rounding to the cipher key blocksize |
55 | // the tricky part is rounding to the cipher key blocksize |
55 | int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; |
56 | int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; |
56 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
57 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
57 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
58 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
58 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
59 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
59 | |
60 | |
60 | char *env; |
61 | char *env; |
61 | asprintf (&env, "CONFBASE=%s", confbase); putenv (env); |
62 | asprintf (&env, "CONFBASE=%s", confbase); putenv (env); |
62 | asprintf (&env, "NODENAME=%s", THISNODE->nodename); putenv (env); |
|
|
63 | asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env); |
|
|
64 | asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); |
63 | asprintf (&env, "IFNAME=%s", tap->interface ()); putenv (env); |
65 | asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env); |
64 | asprintf (&env, "IFTYPE=%s", IFTYPE); putenv (env); |
66 | asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env); |
65 | asprintf (&env, "IFSUBTYPE=%s", IFSUBTYPE); putenv (env); |
67 | asprintf (&env, "MTU=%d", mtu); putenv (env); |
66 | asprintf (&env, "MTU=%d", mtu); putenv (env); |
68 | asprintf (&env, "MAC=%02x:%02x:%02x:%02x:%02x:%02x", |
67 | asprintf (&env, "NODES=%d", conns.size ()); putenv (env); |
69 | 0xfe, 0xfd, 0x80, 0x00, THISNODE->id >> 8, |
68 | asprintf (&env, "NODEID=%d", THISNODE->id); putenv (env); |
70 | THISNODE->id & 0xff); |
|
|
71 | putenv (env); |
|
|
72 | |
69 | |
|
|
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 | } |
|
|
78 | } |
|
|
79 | |
|
|
80 | const char *vpn::script_if_init () |
|
|
81 | { |
|
|
82 | script_init_env (); |
|
|
83 | |
|
|
84 | return tap->if_up (); |
|
|
85 | } |
|
|
86 | |
|
|
87 | const char *vpn::script_if_up () |
|
|
88 | { |
|
|
89 | script_init_env (); |
|
|
90 | |
|
|
91 | char *filename; |
|
|
92 | asprintf (&filename, |
|
|
93 | "%s/%s", |
|
|
94 | confbase, |
73 | return ::conf.script_if_up ? ::conf.script_if_up : "if-up"; |
95 | ::conf.script_if_up ? ::conf.script_if_up : "if-up"); |
|
|
96 | |
|
|
97 | return filename; |
74 | } |
98 | } |
75 | |
99 | |
76 | int |
100 | int |
77 | vpn::setup () |
101 | vpn::setup () |
78 | { |
102 | { |
… | |
… | |
84 | |
108 | |
85 | if (ipv4_fd < 0) |
109 | if (ipv4_fd < 0) |
86 | return -1; |
110 | return -1; |
87 | |
111 | |
88 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
112 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
|
|
113 | fcntl (ipv4_fd, F_SETFD, FD_CLOEXEC); |
89 | |
114 | |
90 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
115 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
91 | // this I really consider a linux bug. I am neither connected |
116 | // this I really consider a linux bug. I am neither connected |
92 | // 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 |
93 | // fragment for me sometimes. |
118 | // fragment for me sometimes. |
… | |
… | |
116 | |
141 | |
117 | if (udpv4_fd < 0) |
142 | if (udpv4_fd < 0) |
118 | return -1; |
143 | return -1; |
119 | |
144 | |
120 | fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
145 | fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
146 | fcntl (udpv4_fd, F_SETFD, FD_CLOEXEC); |
121 | |
147 | |
122 | // standard daemon practise... |
148 | // standard daemon practise... |
123 | { |
149 | { |
124 | int oval = 1; |
150 | int oval = 1; |
125 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
151 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
… | |
… | |
155 | |
181 | |
156 | if (icmpv4_fd < 0) |
182 | if (icmpv4_fd < 0) |
157 | return -1; |
183 | return -1; |
158 | |
184 | |
159 | fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
185 | fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
186 | fcntl (icmpv4_fd, F_SETFD, FD_CLOEXEC); |
160 | |
187 | |
161 | #ifdef ICMP_FILTER |
188 | #ifdef ICMP_FILTER |
162 | { |
189 | { |
163 | icmp_filter oval; |
190 | icmp_filter oval; |
164 | oval.data = 0xffffffff; |
191 | oval.data = 0xffffffff; |
… | |
… | |
173 | // this I really consider a linux bug. I am neither connected |
200 | // this I really consider a linux bug. I am neither connected |
174 | // 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 |
175 | // fragment for me sometimes. |
202 | // fragment for me sometimes. |
176 | { |
203 | { |
177 | int oval = IP_PMTUDISC_DONT; |
204 | int oval = IP_PMTUDISC_DONT; |
178 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
205 | setsockopt (icmpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
179 | } |
206 | } |
180 | #endif |
207 | #endif |
181 | |
208 | |
182 | sockinfo si (THISNODE, PROT_ICMPv4); |
209 | sockinfo si (THISNODE, PROT_ICMPv4); |
183 | |
210 | |
… | |
… | |
200 | |
227 | |
201 | if (tcpv4_fd < 0) |
228 | if (tcpv4_fd < 0) |
202 | return -1; |
229 | return -1; |
203 | |
230 | |
204 | fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
231 | fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
232 | fcntl (tcpv4_fd, F_SETFD, FD_CLOEXEC); |
205 | |
233 | |
206 | // standard daemon practise... |
234 | // standard daemon practise... |
207 | { |
235 | { |
208 | int oval = 1; |
236 | int oval = 1; |
209 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
237 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
… | |
… | |
235 | dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
263 | dnsv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
236 | |
264 | |
237 | if (dnsv4_fd < 0) |
265 | if (dnsv4_fd < 0) |
238 | return -1; |
266 | return -1; |
239 | |
267 | |
|
|
268 | fcntl (dnsv4_fd, F_SETFL, O_NONBLOCK); |
|
|
269 | fcntl (dnsv4_fd, F_SETFD, FD_CLOEXEC); |
|
|
270 | |
|
|
271 | # if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
|
|
272 | // this I really consider a linux bug. I am neither connected |
|
|
273 | // nor do I fragment myself. Linux still sets DF and doesn't |
|
|
274 | // fragment for me sometimes. |
|
|
275 | { |
|
|
276 | int oval = IP_PMTUDISC_DONT; |
|
|
277 | setsockopt (dnsv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
|
|
278 | } |
|
|
279 | # endif |
|
|
280 | |
240 | // standard daemon practise... |
281 | // standard daemon practise... |
241 | { |
282 | { |
242 | int oval = 1; |
283 | int oval = 1; |
243 | setsockopt (dnsv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
284 | setsockopt (dnsv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
244 | } |
285 | } |
… | |
… | |
255 | |
296 | |
256 | dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); |
297 | dnsv4_ev_watcher.start (dnsv4_fd, EVENT_READ); |
257 | } |
298 | } |
258 | #endif |
299 | #endif |
259 | |
300 | |
|
|
301 | ///////////////////////////////////////////////////////////////////////////// |
|
|
302 | |
|
|
303 | reconnect_all (); |
|
|
304 | |
|
|
305 | ///////////////////////////////////////////////////////////////////////////// |
|
|
306 | |
260 | tap = new tap_device (); |
307 | tap = new tap_device (); |
261 | if (!tap) //D this, of course, never catches |
308 | if (!tap) //D this, of course, never catches |
262 | { |
309 | { |
263 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
310 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
264 | exit (EXIT_FAILURE); |
311 | exit (EXIT_FAILURE); |
265 | } |
312 | } |
266 | |
313 | |
|
|
314 | fcntl (tap->fd, F_SETFD, FD_CLOEXEC); |
|
|
315 | |
|
|
316 | if (tap->if_up () && |
|
|
317 | !run_script (run_script_cb (this, &vpn::script_if_init), true)) |
|
|
318 | { |
|
|
319 | slog (L_ERR, _("interface initialization command '%s' failed, exiting."), |
|
|
320 | tap->if_up ()); |
|
|
321 | exit (EXIT_FAILURE); |
|
|
322 | } |
|
|
323 | |
267 | run_script (run_script_cb (this, &vpn::script_if_up), true); |
324 | if (!run_script (run_script_cb (this, &vpn::script_if_up), true)) |
|
|
325 | { |
|
|
326 | slog (L_ERR, _("if-up command execution failed, exiting.")); |
|
|
327 | exit (EXIT_FAILURE); |
|
|
328 | } |
268 | |
329 | |
269 | tap_ev_watcher.start (tap->fd, EVENT_READ); |
330 | tap_ev_watcher.start (tap->fd, EVENT_READ); |
270 | |
|
|
271 | reconnect_all (); |
|
|
272 | |
331 | |
273 | return 0; |
332 | return 0; |
274 | } |
333 | } |
275 | |
334 | |
276 | bool |
335 | bool |