… | |
… | |
26 | |
26 | |
27 | #include <sys/types.h> |
27 | #include <sys/types.h> |
28 | #include <sys/socket.h> |
28 | #include <sys/socket.h> |
29 | #include <sys/poll.h> |
29 | #include <sys/poll.h> |
30 | #include <sys/wait.h> |
30 | #include <sys/wait.h> |
31 | #include <netinet/in.h> |
|
|
32 | #include <arpa/inet.h> |
|
|
33 | #include <errno.h> |
31 | #include <errno.h> |
34 | #include <time.h> |
32 | #include <time.h> |
35 | #include <unistd.h> |
33 | #include <unistd.h> |
|
|
34 | #include <fcntl.h> |
|
|
35 | #include <arpa/inet.h> |
|
|
36 | #include <netinet/in.h> |
|
|
37 | #include <netinet/ip_icmp.h> |
36 | |
38 | |
37 | #include "pidfile.h" |
39 | #include "pidfile.h" |
38 | |
40 | |
39 | #include "connection.h" |
41 | #include "connection.h" |
40 | #include "util.h" |
42 | #include "util.h" |
… | |
… | |
71 | } |
73 | } |
72 | |
74 | |
73 | int |
75 | int |
74 | vpn::setup () |
76 | vpn::setup () |
75 | { |
77 | { |
|
|
78 | ipv4_fd = -1; |
|
|
79 | |
|
|
80 | if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto) |
|
|
81 | { |
|
|
82 | ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
|
|
83 | |
|
|
84 | if (ipv4_fd < 0) |
|
|
85 | return -1; |
|
|
86 | |
|
|
87 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
|
|
88 | |
|
|
89 | #ifdef IP_MTU_DISCOVER |
|
|
90 | // this I really consider a linux bug. I am neither connected |
|
|
91 | // nor do I fragment myself. Linux still sets DF and doesn't |
|
|
92 | // fragment for me sometimes. |
|
|
93 | { |
|
|
94 | int oval = IP_PMTUDISC_DONT; |
|
|
95 | setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
|
|
96 | } |
|
|
97 | #endif |
|
|
98 | |
|
|
99 | sockinfo si (THISNODE, PROT_IPv4); |
|
|
100 | |
|
|
101 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
102 | { |
|
|
103 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
|
|
104 | exit (1); |
|
|
105 | } |
|
|
106 | |
|
|
107 | ipv4_ev_watcher.start (ipv4_fd, POLLIN); |
|
|
108 | } |
|
|
109 | |
76 | udpv4_fd = -1; |
110 | udpv4_fd = -1; |
77 | |
111 | |
78 | if (THISNODE->protocols & PROT_UDPv4) |
112 | if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) |
79 | { |
113 | { |
80 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
114 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
81 | |
115 | |
82 | if (udpv4_fd < 0) |
116 | if (udpv4_fd < 0) |
83 | return -1; |
117 | return -1; |
|
|
118 | |
|
|
119 | fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
84 | |
120 | |
85 | // standard daemon practise... |
121 | // standard daemon practise... |
86 | { |
122 | { |
87 | int oval = 1; |
123 | int oval = 1; |
88 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
124 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
89 | } |
125 | } |
90 | |
|
|
91 | sockinfo si (THISNODE, PROT_UDPv4); |
|
|
92 | |
|
|
93 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
94 | { |
|
|
95 | slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
96 | exit (1); |
|
|
97 | } |
|
|
98 | |
126 | |
99 | #ifdef IP_MTU_DISCOVER |
127 | #ifdef IP_MTU_DISCOVER |
100 | // this I really consider a linux bug. I am neither connected |
128 | // this I really consider a linux bug. I am neither connected |
101 | // nor do I fragment myself. Linux still sets DF and doesn't |
129 | // nor do I fragment myself. Linux still sets DF and doesn't |
102 | // fragment for me sometimes. |
130 | // fragment for me sometimes. |
… | |
… | |
104 | int oval = IP_PMTUDISC_DONT; |
132 | int oval = IP_PMTUDISC_DONT; |
105 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
133 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
106 | } |
134 | } |
107 | #endif |
135 | #endif |
108 | |
136 | |
|
|
137 | sockinfo si (THISNODE, PROT_UDPv4); |
|
|
138 | |
|
|
139 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
140 | { |
|
|
141 | slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
142 | exit (1); |
|
|
143 | } |
|
|
144 | |
109 | udpv4_ev_watcher.start (udpv4_fd, POLLIN); |
145 | udpv4_ev_watcher.start (udpv4_fd, POLLIN); |
110 | } |
146 | } |
111 | |
147 | |
112 | ipv4_fd = -1; |
148 | icmpv4_fd = -1; |
|
|
149 | |
|
|
150 | #if ENABLE_ICMP |
113 | if (THISNODE->protocols & PROT_IPv4) |
151 | if (THISNODE->protocols & PROT_ICMPv4) |
114 | { |
152 | { |
115 | ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
153 | icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); |
116 | |
154 | |
117 | if (ipv4_fd < 0) |
155 | if (icmpv4_fd < 0) |
118 | return -1; |
156 | return -1; |
119 | |
157 | |
120 | sockinfo si (THISNODE, PROT_IPv4); |
158 | fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
121 | |
159 | |
122 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
160 | #ifdef ICMP_FILTER |
123 | { |
161 | { |
124 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
162 | icmp_filter oval; |
125 | exit (1); |
163 | oval.data = 0xffffffff; |
|
|
164 | if (::conf.icmp_type < 32) |
|
|
165 | oval.data &= ~(1 << ::conf.icmp_type); |
|
|
166 | |
|
|
167 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
126 | } |
168 | } |
|
|
169 | #endif |
127 | |
170 | |
128 | #ifdef IP_MTU_DISCOVER |
171 | #ifdef IP_MTU_DISCOVER |
129 | // this I really consider a linux bug. I am neither connected |
172 | // this I really consider a linux bug. I am neither connected |
130 | // nor do I fragment myself. Linux still sets DF and doesn't |
173 | // nor do I fragment myself. Linux still sets DF and doesn't |
131 | // fragment for me sometimes. |
174 | // fragment for me sometimes. |
132 | { |
175 | { |
133 | int oval = IP_PMTUDISC_DONT; |
176 | int oval = IP_PMTUDISC_DONT; |
134 | setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
177 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
135 | } |
178 | } |
136 | #endif |
179 | #endif |
137 | |
180 | |
|
|
181 | sockinfo si (THISNODE, PROT_ICMPv4); |
|
|
182 | |
|
|
183 | if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
184 | { |
|
|
185 | slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
186 | exit (1); |
|
|
187 | } |
|
|
188 | |
138 | ipv4_ev_watcher.start (ipv4_fd, POLLIN); |
189 | icmpv4_ev_watcher.start (icmpv4_fd, POLLIN); |
139 | } |
190 | } |
|
|
191 | #endif |
|
|
192 | |
|
|
193 | tcpv4_fd = -1; |
140 | |
194 | |
141 | #if ENABLE_TCP |
195 | #if ENABLE_TCP |
142 | if (THISNODE->protocols & PROT_TCPv4) |
196 | if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port) |
143 | { |
197 | { |
144 | tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
198 | tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
145 | |
199 | |
146 | if (tcpv4_fd < 0) |
200 | if (tcpv4_fd < 0) |
147 | return -1; |
201 | return -1; |
|
|
202 | |
|
|
203 | fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
148 | |
204 | |
149 | // standard daemon practise... |
205 | // standard daemon practise... |
150 | { |
206 | { |
151 | int oval = 1; |
207 | int oval = 1; |
152 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
208 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
… | |
… | |
185 | |
241 | |
186 | return 0; |
242 | return 0; |
187 | } |
243 | } |
188 | |
244 | |
189 | // send a vpn packet out to other hosts |
245 | // send a vpn packet out to other hosts |
190 | void |
246 | bool |
191 | vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
247 | vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
192 | { |
248 | { |
193 | switch (si.prot) |
249 | switch (si.prot) |
194 | { |
250 | { |
195 | case PROT_IPv4: |
251 | case PROT_IPv4: |
196 | send_ipv4_packet (pkt, si, tos); |
252 | return send_ipv4_packet (pkt, si, tos); |
197 | break; |
|
|
198 | |
253 | |
199 | case PROT_UDPv4: |
254 | case PROT_UDPv4: |
200 | send_udpv4_packet (pkt, si, tos); |
255 | return send_udpv4_packet (pkt, si, tos); |
201 | break; |
|
|
202 | |
256 | |
203 | #if ENABLE_TCP |
257 | #if ENABLE_TCP |
204 | case PROT_TCPv4: |
258 | case PROT_TCPv4: |
205 | send_tcpv4_packet (pkt, si, tos); |
259 | return send_tcpv4_packet (pkt, si, tos); |
206 | break; |
260 | #endif |
|
|
261 | |
|
|
262 | #if ENABLE_ICMP |
|
|
263 | case PROT_ICMPv4: |
|
|
264 | return send_icmpv4_packet (pkt, si, tos); |
207 | #endif |
265 | #endif |
208 | |
266 | |
209 | default: |
267 | default: |
210 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
268 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
211 | abort (); |
269 | return false; |
212 | } |
270 | } |
213 | } |
271 | } |
214 | |
272 | |
215 | void |
273 | bool |
216 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
274 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
217 | { |
275 | { |
218 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
276 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
219 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
277 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
220 | } |
|
|
221 | |
278 | |
222 | void |
279 | return true; |
|
|
280 | } |
|
|
281 | |
|
|
282 | static u16 |
|
|
283 | ipv4_checksum (u16 *data, unsigned int len) |
|
|
284 | { |
|
|
285 | // use 32 bit accumulator and fold back carry bits at the end |
|
|
286 | u32 sum = 0; |
|
|
287 | |
|
|
288 | while (len > 1) |
|
|
289 | { |
|
|
290 | sum += *data++; |
|
|
291 | len -= 2; |
|
|
292 | } |
|
|
293 | |
|
|
294 | // odd byte left? |
|
|
295 | if (len) |
|
|
296 | sum += *(u8 *)data; |
|
|
297 | |
|
|
298 | // add back carry bits |
|
|
299 | sum = (sum >> 16) + (sum & 0xffff); // lo += hi |
|
|
300 | sum += (sum >> 16); // carry |
|
|
301 | |
|
|
302 | return ~sum; |
|
|
303 | } |
|
|
304 | |
|
|
305 | bool |
|
|
306 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
|
|
307 | { |
|
|
308 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
309 | |
|
|
310 | pkt->unshift_hdr (4); |
|
|
311 | |
|
|
312 | icmphdr *hdr = (icmphdr *)&((*pkt)[0]); |
|
|
313 | hdr->type = ::conf.icmp_type; |
|
|
314 | hdr->code = 255; |
|
|
315 | hdr->checksum = 0; |
|
|
316 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
|
|
317 | |
|
|
318 | sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
|
|
319 | |
|
|
320 | return true; |
|
|
321 | } |
|
|
322 | |
|
|
323 | bool |
223 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
324 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
224 | { |
325 | { |
225 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
326 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
226 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
327 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
|
|
328 | |
|
|
329 | return true; |
227 | } |
330 | } |
228 | |
331 | |
229 | void |
332 | void |
230 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
333 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
231 | { |
334 | { |
… | |
… | |
238 | if (src == 0 || src > conns.size () |
341 | if (src == 0 || src > conns.size () |
239 | || dst > conns.size () |
342 | || dst > conns.size () |
240 | || pkt->typ () >= vpn_packet::PT_MAX) |
343 | || pkt->typ () >= vpn_packet::PT_MAX) |
241 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
344 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
242 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
345 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
346 | else if (dst > conns.size ()) |
|
|
347 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
|
|
348 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
243 | else |
349 | else |
244 | { |
350 | { |
245 | connection *c = conns[src - 1]; |
351 | connection *c = conns[src - 1]; |
246 | |
352 | |
247 | if (dst == 0 && !THISNODE->routerprio) |
353 | if (dst == 0 && !THISNODE->routerprio) |
248 | slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), |
354 | slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), |
249 | c->conf->nodename, (const char *)rsi); |
355 | c->conf->nodename, (const char *)rsi); |
250 | else if (dst != 0 && dst != THISNODE->id) |
356 | else if (dst != 0 && dst != THISNODE->id) |
251 | // FORWARDING NEEDED ;) |
357 | { |
|
|
358 | if (THISNODE->routerprio) |
|
|
359 | // the tos setting gets lost here. who cares. |
|
|
360 | conns[dst - 1]->inject_vpn_packet (pkt); |
|
|
361 | else |
252 | slog (L_WARN, |
362 | slog (L_WARN, |
253 | _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"), |
363 | _("%s(%s): forwarding request (=> %s), but we are no router"), |
|
|
364 | c->conf->nodename, (const char *)rsi, |
254 | dst, conns[dst - 1]->conf->nodename, |
365 | conns[dst - 1]->conf->nodename); |
255 | (const char *)rsi, |
366 | } |
256 | THISNODE->id, THISNODE->nodename); |
|
|
257 | else |
367 | else |
258 | c->recv_vpn_packet (pkt, rsi); |
368 | c->recv_vpn_packet (pkt, rsi); |
259 | } |
369 | } |
260 | } |
370 | } |
261 | |
371 | |
262 | void |
372 | void |
263 | vpn::udpv4_ev (io_watcher &w, short revents) |
373 | vpn::ipv4_ev (io_watcher &w, short revents) |
264 | { |
374 | { |
265 | if (revents & (POLLIN | POLLERR)) |
375 | if (revents & (POLLIN | POLLERR)) |
266 | { |
376 | { |
267 | vpn_packet *pkt = new vpn_packet; |
377 | vpn_packet *pkt = new vpn_packet; |
268 | struct sockaddr_in sa; |
378 | struct sockaddr_in sa; |
269 | socklen_t sa_len = sizeof (sa); |
379 | socklen_t sa_len = sizeof (sa); |
270 | int len; |
380 | int len; |
271 | |
381 | |
272 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
382 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
273 | |
383 | |
274 | sockinfo si(sa, PROT_UDPv4); |
384 | sockinfo si(sa, PROT_IPv4); |
275 | |
385 | |
276 | if (len > 0) |
386 | if (len > 0) |
277 | { |
387 | { |
278 | pkt->len = len; |
388 | pkt->len = len; |
279 | |
389 | |
|
|
390 | // raw sockets deliver the ipv4, but don't expect it on sends |
|
|
391 | // this is slow, but... |
|
|
392 | pkt->skip_hdr (IP_OVERHEAD); |
|
|
393 | |
280 | recv_vpn_packet (pkt, si); |
394 | recv_vpn_packet (pkt, si); |
281 | } |
395 | } |
282 | else |
396 | else |
283 | { |
397 | { |
284 | // probably ECONNRESET or somesuch |
398 | // probably ECONNRESET or somesuch |
285 | slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); |
399 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
286 | } |
400 | } |
287 | |
401 | |
288 | delete pkt; |
402 | delete pkt; |
289 | } |
403 | } |
290 | else if (revents & POLLHUP) |
404 | else if (revents & POLLHUP) |
291 | { |
405 | { |
292 | // this cannot ;) happen on udp sockets |
406 | // this cannot ;) happen on udp sockets |
293 | slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); |
407 | slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); |
294 | exit (1); |
408 | exit (1); |
295 | } |
409 | } |
296 | else |
410 | else |
297 | { |
411 | { |
298 | slog (L_ERR, |
412 | slog (L_ERR, |
… | |
… | |
301 | exit (1); |
415 | exit (1); |
302 | } |
416 | } |
303 | } |
417 | } |
304 | |
418 | |
305 | void |
419 | void |
306 | vpn::ipv4_ev (io_watcher &w, short revents) |
420 | vpn::icmpv4_ev (io_watcher &w, short revents) |
307 | { |
421 | { |
308 | if (revents & (POLLIN | POLLERR)) |
422 | if (revents & (POLLIN | POLLERR)) |
309 | { |
423 | { |
310 | vpn_packet *pkt = new vpn_packet; |
424 | vpn_packet *pkt = new vpn_packet; |
311 | struct sockaddr_in sa; |
425 | struct sockaddr_in sa; |
312 | socklen_t sa_len = sizeof (sa); |
426 | socklen_t sa_len = sizeof (sa); |
313 | int len; |
427 | int len; |
314 | |
428 | |
315 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
429 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
316 | |
430 | |
317 | sockinfo si(sa, PROT_IPv4); |
431 | sockinfo si(sa, PROT_ICMPv4); |
318 | |
432 | |
319 | if (len > 0) |
433 | if (len > 0) |
320 | { |
434 | { |
321 | pkt->len = len; |
435 | pkt->len = len; |
322 | |
436 | |
|
|
437 | icmphdr *hdr = (icmphdr *)&((*pkt)[IP_OVERHEAD]); |
|
|
438 | |
|
|
439 | if (hdr->type == ::conf.icmp_type |
|
|
440 | && hdr->code == 255) |
|
|
441 | { |
323 | // raw sockets deliver the ipv4, but don't expect it on sends |
442 | // raw sockets deliver the ipv4, but don't expect it on sends |
324 | // this is slow, but... |
443 | // this is slow, but... |
325 | pkt->skip_hdr (IP_OVERHEAD); |
444 | pkt->skip_hdr (ICMP_OVERHEAD); |
326 | |
445 | |
327 | recv_vpn_packet (pkt, si); |
446 | recv_vpn_packet (pkt, si); |
|
|
447 | } |
328 | } |
448 | } |
329 | else |
449 | else |
330 | { |
450 | { |
331 | // probably ECONNRESET or somesuch |
451 | // probably ECONNRESET or somesuch |
332 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
452 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
… | |
… | |
335 | delete pkt; |
455 | delete pkt; |
336 | } |
456 | } |
337 | else if (revents & POLLHUP) |
457 | else if (revents & POLLHUP) |
338 | { |
458 | { |
339 | // this cannot ;) happen on udp sockets |
459 | // this cannot ;) happen on udp sockets |
340 | slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); |
460 | slog (L_ERR, _("FATAL: POLLHUP on icmpv4 fd, terminating.")); |
|
|
461 | exit (1); |
|
|
462 | } |
|
|
463 | else |
|
|
464 | { |
|
|
465 | slog (L_ERR, |
|
|
466 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
|
|
467 | revents); |
|
|
468 | exit (1); |
|
|
469 | } |
|
|
470 | } |
|
|
471 | |
|
|
472 | void |
|
|
473 | vpn::udpv4_ev (io_watcher &w, short revents) |
|
|
474 | { |
|
|
475 | if (revents & (POLLIN | POLLERR)) |
|
|
476 | { |
|
|
477 | vpn_packet *pkt = new vpn_packet; |
|
|
478 | struct sockaddr_in sa; |
|
|
479 | socklen_t sa_len = sizeof (sa); |
|
|
480 | int len; |
|
|
481 | |
|
|
482 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
|
|
483 | |
|
|
484 | sockinfo si(sa, PROT_UDPv4); |
|
|
485 | |
|
|
486 | if (len > 0) |
|
|
487 | { |
|
|
488 | pkt->len = len; |
|
|
489 | |
|
|
490 | recv_vpn_packet (pkt, si); |
|
|
491 | } |
|
|
492 | else |
|
|
493 | { |
|
|
494 | // probably ECONNRESET or somesuch |
|
|
495 | slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); |
|
|
496 | } |
|
|
497 | |
|
|
498 | delete pkt; |
|
|
499 | } |
|
|
500 | else if (revents & POLLHUP) |
|
|
501 | { |
|
|
502 | // this cannot ;) happen on udp sockets |
|
|
503 | slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); |
341 | exit (1); |
504 | exit (1); |
342 | } |
505 | } |
343 | else |
506 | else |
344 | { |
507 | { |
345 | slog (L_ERR, |
508 | slog (L_ERR, |
… | |
… | |
469 | } |
632 | } |
470 | } |
633 | } |
471 | |
634 | |
472 | connection *vpn::find_router () |
635 | connection *vpn::find_router () |
473 | { |
636 | { |
474 | u32 prio = 0; |
637 | u32 prio = 1; |
475 | connection *router = 0; |
638 | connection *router = 0; |
476 | |
639 | |
477 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
640 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
478 | { |
641 | { |
479 | connection *c = *i; |
642 | connection *c = *i; |
480 | |
643 | |
481 | if (c->conf->routerprio > prio |
644 | if (c->conf->routerprio >= prio |
482 | && c->connectmode == conf_node::C_ALWAYS |
645 | && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use |
483 | && c->conf != THISNODE |
|
|
484 | && c->ictx && c->octx) |
646 | && c->ictx && c->octx |
|
|
647 | && c->conf != THISNODE // redundant, since ictx==octx==0 always on thisnode |
|
|
648 | && (!THISNODE->routerprio |
|
|
649 | || c->conf->routerprio <= THISNODE->routerprio)) |
485 | { |
650 | { |
486 | prio = c->conf->routerprio; |
651 | prio = c->conf->routerprio; |
487 | router = c; |
652 | router = c; |
488 | } |
653 | } |
489 | } |
654 | } |
490 | |
655 | |
491 | return router; |
656 | return router; |
492 | } |
657 | } |
493 | |
658 | |
494 | void vpn::connect_request (int id) |
659 | void vpn::send_connect_request (int id) |
495 | { |
660 | { |
496 | connection *c = find_router (); |
661 | connection *c = find_router (); |
497 | |
662 | |
498 | if (c) |
663 | if (c) |
499 | c->connect_request (id); |
664 | c->send_connect_request (id); |
500 | //else // does not work, because all others must connect to the same router |
665 | else |
501 | // // no router found, aggressively connect to all routers |
666 | // no router found, aggressively connect to all routers |
502 | // for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
667 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
503 | // if ((*i)->conf->routerprio) |
668 | if ((*i)->conf->routerprio) |
504 | // (*i)->establish_connection (); |
669 | (*i)->establish_connection (); |
505 | } |
670 | } |
506 | |
671 | |
507 | void |
672 | void |
508 | connection::dump_status () |
673 | connection::dump_status () |
509 | { |
674 | { |
… | |
… | |
526 | |
691 | |
527 | slog (L_NOTICE, _("END status dump")); |
692 | slog (L_NOTICE, _("END status dump")); |
528 | } |
693 | } |
529 | |
694 | |
530 | vpn::vpn (void) |
695 | vpn::vpn (void) |
531 | : event(this, &vpn::event_cb) |
696 | : event (this, &vpn::event_cb) |
532 | , udpv4_ev_watcher(this, &vpn::udpv4_ev) |
697 | , udpv4_ev_watcher (this, &vpn::udpv4_ev) |
533 | , ipv4_ev_watcher (this, &vpn::ipv4_ev) |
698 | , ipv4_ev_watcher (this, &vpn::ipv4_ev) |
534 | , tap_ev_watcher (this, &vpn::tap_ev) |
|
|
535 | #if ENABLE_TCP |
699 | #if ENABLE_TCP |
536 | , tcpv4_ev_watcher(this, &vpn::tcpv4_ev) |
700 | , tcpv4_ev_watcher (this, &vpn::tcpv4_ev) |
537 | #endif |
701 | #endif |
|
|
702 | #if ENABLE_ICMP |
|
|
703 | , icmpv4_ev_watcher(this, &vpn::icmpv4_ev) |
|
|
704 | #endif |
|
|
705 | , tap_ev_watcher (this, &vpn::tap_ev) |
538 | { |
706 | { |
539 | } |
707 | } |
540 | |
708 | |
541 | vpn::~vpn () |
709 | vpn::~vpn () |
542 | { |
710 | { |